Option
(Some
, None
)
match
, if let
, "ръкави")
ref
, ref mut
)TODO израз със pattern-matching с "намерете трите проблема":
Вече сме ги виждали
Option<T>
Vec<T>
fn max(list: &[i32]) -> i32 {
let mut max = list[0];
for &item in list.iter() {
if item > max {
max = item;
}
}
max
}
fn main() {
let numbers = [1337, 42, 94, 280];
println!("Max: {}", max(numbers));
}
fn main() {
let numbers = [1337, 42, 94, 280];
println!("Max: {}", max(numbers));
let numbers = [3.14, 1.337];
println!("Max: {}", max(numbers)); // компилационна грешка!
}
fn max_i32(list: &[i32]) -> i32 {
// ...
}
fn max_f32(list: &[f32]) -> f32 {
// ...
}
fn max<T>(list: &[T]) -> T {
// ...
}
error[E0369]: binary operation `>` cannot be applied to type `T` | 5 | if item > max { | ^^^^ | note: an implementation of `std::cmp::PartialOrd` might be missing for `T`