You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fnlargest<T>(list:&[T]) -> T{letmut largest = list[0];for&item in list.iter(){if item > largest {
largest = item;}}
largest
}fnmain(){let number_list = vec![34, 50, 25, 100, 65];let result = largest(&number_list);println!("The largest number is {}", result);let char_list = vec!['y', 'm', 'a', 'q'];let result = largest(&char_list);println!("The largest char is {}", result);}
这段代码即使补充上后文所述的 : std::cmp::PartialOrd 也仍会报下面的错误:
error[E0508]: cannot move out of type `[T]`, a non-copy slice
--> src/main.rs:2:23
|
2 | let mut largest = list[0];
| ^^^^^^^
| |
| cannot move out of here
| move occurs because `list[_]` has type `T`, which does not implement the `Copy` trait
| help: consider borrowing here: `&list[0]`
一个可行的示例:
fnlargest<T: std::cmp::PartialOrd>(list:&[T]) -> &T{letmut largest = &list[0];for item in list.iter(){if item > largest {
largest = item
}}
largest
}fnmain(){let number_list = vec![34, 50, 25, 100, 65];let result = largest(&number_list);println!("The largest number is {}", result);let char_list = vec!['y', 'm', 'a', 'q'];let result = largest(&char_list);println!("The largest char is {}", result);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
这段代码即使补充上后文所述的
: std::cmp::PartialOrd
也仍会报下面的错误:一个可行的示例:
Rust 版本:
rustc 1.59.0 (9d1b2106e 2022-02-23)
Beta Was this translation helpful? Give feedback.
All reactions