我正在尝试创建一个对数字排序的函数数组,但是声明数组的类型有问题。我不能让它在函数的输入参数上是泛型的。
如何实现数组sorts的函数,其第一个也是唯一的参数只需要实现特征Ord和Copy,而不是u32
// What I have now. Note the fn(&mut [u32])
let sorts: [(&'static str, fn(&mut [u32])); 6] = [
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
];
fn insertion_sort(array: &mut [impl Ord + Copy]) { }
fn selection_sort(array: &mut [impl Ord + Copy]) { }
fn bubble_sort(array: &mut [impl Ord + Copy]) { }
fn merge_sort(array: &mut [impl Ord + Copy]) { }
fn quick_sort(array: &mut [impl Ord + Copy]) { }
fn heap_sort(array: &mut [impl Ord + Copy]) { }
// What I want to accomplish. Note the fn(&mut [impl Ord + Copy])
let sorts: [(&'static str, fn(&mut [impl Ord + Copy])); 6] = [
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
];我想这样做的原因是,在将排序函数数组应用于不同类型的数字数组时,不需要更改排序函数数组的类型。
编辑:我想完成以下工作。基本上,我希望通过将它们依次应用于前面定义的数组来测试不同的排序实现。sorts数组如下所示,包括syskov提供的答案。
fn main() {
let problem: [u8; 32] = rand::random();
fn create_sorts<T: Ord + Copy>() -> [(&'static str, fn(&mut [T])); 7] {
[
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
("Stooge sort", stooge_sort),
]
}
println!("{:?}", problem);
for (name, sort) in create_sorts().iter() {
let mut problem_ = problem.clone();
let now = Instant::now();
sort(&mut problem_);
let elapsed = now.elapsed();
let judgment: &str = match is_sorted(&problem_) {
true => "✓",
false => "✗",
};
println!("{} in {:?}: {}", judgment, elapsed, name);
}
}发布于 2020-01-03 11:44:38
我不知道使用排序的确切上下文,但是您可以根据排序的数字数组拥有某种泛型函数。这会减少样板。
fn main () {
fn insertion_sort(array: &mut [impl Ord + Copy]) { }
fn selection_sort(array: &mut [impl Ord + Copy]) { }
fn bubble_sort(array: &mut [impl Ord + Copy]) { }
fn merge_sort(array: &mut [impl Ord + Copy]) { }
fn quick_sort(array: &mut [impl Ord + Copy]) { }
fn heap_sort(array: &mut [impl Ord + Copy]) { }
fn create_sorts<T: Ord + Copy>() -> [(&'static str, fn (&mut [T])); 6] {
[
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
]
}
let sorts_u32 = create_sorts::<u32>();
let sorts_u64 = create_sorts::<u64>();
}发布于 2020-01-03 11:50:55
需要有一个泛型函数或结构来存储数组。类似于下面的例子。
fn dumb<T>()
where T : Ord + Copy
{
// What I want to accomplish. Note the fn(&mut [impl Ord + Copy])
let sorts: [(&'static str, fn(&mut [T])); 6] = [
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
];
}struct Sorts<T>
where T : Ord + Copy
{
sorts : [(&'static str, fn(&mut [T])); 6]
}
impl<T> Sorts<T>
where T : Ord + Copy
{
pub fn new() -> Self {
Sorts {
sorts : [
("Insertion sort", insertion_sort),
("Selection sort", selection_sort),
("Bubble sort", bubble_sort),
("Merge sort", merge_sort),
("Quick sort", quick_sort),
("Heap sort", heap_sort),
]
}
}
}https://stackoverflow.com/questions/59576952
复制相似问题