Generics 如何将特性放入锈菌中';数组中的函数类型';s型?

Generics 如何将特性放入锈菌中';数组中的函数类型';s型?,generics,rust,traits,Generics,Rust,Traits,我试图创建一个对数字进行排序的函数数组,但在声明数组的类型时遇到了问题。我不能让它在函数的输入参数上是泛型的 如何实现数组排序的第一个也是唯一一个参数只需要实现traitsOrd和Copy而不是u32的函数 //我现在拥有的。注意fn(&mut[u32]) let排序:[(&'static str,fn(&mut[u32]);6]=[ (“插入排序”,插入排序), (“选择排序”,选择排序), (“气泡排序”,气泡_排序), (“合并排序”,合并排序), (“快速排序”,快速排序), (“堆排序

我试图创建一个对数字进行排序的函数数组,但在声明数组的类型时遇到了问题。我不能让它在函数的输入参数上是泛型的

如何实现数组
排序
的第一个也是唯一一个参数只需要实现traits
Ord
Copy
而不是
u32
的函数

//我现在拥有的。注意fn(&mut[u32])
let排序:[(&'static str,fn(&mut[u32]);6]=[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
];
fn插入\排序(数组:&mut[impl-Ord+Copy]){}
fn选择_排序(数组:&mut[impl-Ord+Copy]){}
fn冒泡排序(数组:&mut[impl-Ord+Copy]){}
fn merge_sort(数组:&mut[impl Ord+Copy]){}
fn快速排序(数组:&mut[impl-Ord+Copy]){}
fn heap_sort(数组:&mut[impl-Ord+Copy]){}
//我想要完成的。注意fn(&mut[impl-Ord+Copy])
让排序:[(&'static str,fn(&mut[impl Ord+Copy]);6]=[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
];
我这样做的原因是,当将排序函数数组应用于不同类型的数字数组时,我不需要更改排序函数数组的类型

编辑:我想完成以下任务。基本上,我希望通过将不同的排序实现依次应用于前面定义的数组,轻松地测试它们。
排序
数组使用如下,包括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);
    }
}
fn main(){
let问题:[u8;32]=rand::random();
fn create_sorts()->[(&'static str,fn(&mut[T]);7]{
[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
(“Stooge排序”,Stooge_排序),
]
}
println!(“{:?}”,问题);
对于create_sorts().iter()中的(名称、排序){
让mut problem_uz=problem.clone();
让现在=瞬间::现在();
排序(&mut问题);
让经过=现在。经过();
判断:&str=match已排序(&problem){
真=>”✓",
假=>”✗",
};
println!({:?}:{}中的“{}”,判断,经过,名称);
}
}

我不知道什么时候使用了排序的确切上下文,但您可以使用某种通用函数,它基于您排序的数字数组。这将简化样板文件

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 main(){
fn插入\排序(数组:&mut[impl-Ord+Copy]){}
fn选择_排序(数组:&mut[impl-Ord+Copy]){}
fn冒泡排序(数组:&mut[impl-Ord+Copy]){}
fn merge_sort(数组:&mut[impl Ord+Copy]){}
fn快速排序(数组:&mut[impl-Ord+Copy]){}
fn heap_sort(数组:&mut[impl-Ord+Copy]){}
fn create_sorts()->[(&'static str,fn(&mut[T]);6]{
[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
]
}
让排序_u32=创建排序::();
让排序_u64=创建排序::();
}

我不知道什么时候使用了排序的确切上下文,但您可以使用某种通用函数,它基于您排序的数字数组。这将简化样板文件

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 main(){
fn插入\排序(数组:&mut[impl-Ord+Copy]){}
fn选择_排序(数组:&mut[impl-Ord+Copy]){}
fn冒泡排序(数组:&mut[impl-Ord+Copy]){}
fn merge_sort(数组:&mut[impl Ord+Copy]){}
fn快速排序(数组:&mut[impl-Ord+Copy]){}
fn heap_sort(数组:&mut[impl-Ord+Copy]){}
fn create_sorts()->[(&'static str,fn(&mut[T]);6]{
[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
]
}
让排序_u32=创建排序::();
让排序_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),
    ];

}
fn dumb()
其中T:Ord+Copy
{
//注意fn(&mut[impl-Ord+Copy])
let排序:[(&'static str,fn(&mut[T]);6]=[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
];
}

struct排序
其中T:Ord+Copy
{
排序:[(&'static str,fn(&mut[T]);6]
}
impl排序
其中T:Ord+Copy
{
pub fn new()->Self{
分类{
分类:[
(“插入排序”,插入排序),
(“选择排序”,选择排序),
(“气泡排序”,气泡_排序),
(“合并排序”,合并排序),
(“快速排序”,快速排序),
(“堆排序”,堆排序),
]
}
}
}

您需要一个通用函数或结构来存储数组。类似于下面的示例

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),
    ];

}
fn dumb()
其中T:Ord+Copy
{
//注意fn(&mut[impl-Ord+Copy])
let排序:[(&'static str,fn(&mut