如何将特征放入数组类型的 Rust 函数类型中?
How to put traits in Rust's function types in an array's type?
我正在尝试创建一个对数字进行排序的函数数组,但在声明数组类型时遇到了问题。我无法让它在函数的输入参数上通用。
我如何实现数组 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);
}
}
我不知道何时使用 sorts 的确切上下文,但是您可以根据您排序的数字数组使用某种通用函数。它会减少样板文件。
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>();
}
您需要一个通用函数或结构来存储您的数组。类似于下面的示例。
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),
]
}
}
}
我正在尝试创建一个对数字进行排序的函数数组,但在声明数组类型时遇到了问题。我无法让它在函数的输入参数上通用。
我如何实现数组 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);
}
}
我不知道何时使用 sorts 的确切上下文,但是您可以根据您排序的数字数组使用某种通用函数。它会减少样板文件。
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>();
}
您需要一个通用函数或结构来存储您的数组。类似于下面的示例。
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),
]
}
}
}