首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Rust函数类型中的特征放在数组的类型中?

如何将Rust函数类型中的特征放在数组的类型中?
EN

Stack Overflow用户
提问于 2020-01-03 10:29:38
回答 2查看 168关注 0票数 1

我正在尝试创建一个对数字排序的函数数组,但是声明数组的类型有问题。我不能让它在函数的输入参数上是泛型的。

如何实现数组sorts的函数,其第一个也是唯一的参数只需要实现特征OrdCopy,而不是u32

代码语言:javascript
复制
// 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提供的答案。

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-03 11:44:38

我不知道使用排序的确切上下文,但是您可以根据排序的数字数组拥有某种泛型函数。这会减少样板。

代码语言:javascript
复制
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>();
}
票数 3
EN

Stack Overflow用户

发布于 2020-01-03 11:50:55

需要有一个泛型函数或结构来存储数组。类似于下面的例子。

代码语言:javascript
复制
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),
    ];

}

代码语言:javascript
复制
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),
            ]
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59576952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档