我的min函数有什么问题?

时间:2016-01-13 10:43:56

标签: rust

我正在尝试编写一个从数组中返回最小数字的函数:

功能定义:

pub fn min(input_array: &[i32]) -> &i32 {
    let x = input_array.iter().min().unwrap();
    x
}

测试:

fn test_min() {
    let input_array = [1, 2, 3, 4];
    assert_eq!(1, functional::min(&input_array));
}

但是当我运行它时,我收到错误:

    <std macros>:5:8: 5:33 error: the trait `core::cmp::PartialEq<&i32>` is not implemented for the type `_` [E0277]
<std macros>:5 if ! ( * left_val == * right_val ) {
                      ^~~~~~~~~~~~~~~~~~~~~~~~~
src/functional.rs:104:5: 104:50 note: in this expansion of assert_eq! (defined in <std macros>)
<std macros>:5:8: 5:33 help: run `rustc --explain E0277` to see a detailed explanation
error: aborting due to previous error
Could not compile `gettingrusty`. 

1 个答案:

答案 0 :(得分:4)

您的min没问题(假设您要返回&i32),错误在assert。 这是正确的:

assert_eq!(&1, min(&input_array));