--> src/main.rs:4:5 | 2 | letx = 5; // solution: let mut x = 5; | - | | | first assignment to `x` | help: consider making this binding mutable: `mut x` 3 | println!("The value of x is {}", x); 4 | x = 6; | ^^^^^ cannot assign twice to immutable variable
consider giving guess a type
编译器无法推断出变量的类型,考虑为这个变量声明一个类型
1 2 3
fnmain() { letguess = "32".trim().parse().expect("Not a number!");// solution: let guess:i32 = "32".trim().parse().expect("Not a number!"); }
--> src/main.rs:2:9 | 2 | letguess = "32".trim().parse().expect("Not a number!");// solution: let guess:i32 = "32".trim().parse().expect("Not a number!"); | ^^^^^ consider giving `guess` a type
index out of bounds: the length is 2 but the index is 2
索引超出范围
1 2 3 4 5
fnmain() { letelements = [1, 2]; letindex = 2; println!("The value of elements is {}", elements[index]); }
1 2 3 4 5 6 7
error: this operation will panic at runtime --> src/main.rs:4:45 | 4 | println!("The value of elements is {}", elements[index]); | ^^^^^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 2 | = note: `#[deny(unconditional_panic)]` on by default
let expressions are not supported here
期待是表达式,但发现是语句
1 2 3
fnmain() { letvalueA = (letvalueB = 6); }
expected expression, found statement (let)
1 2 3 4 5 6 7 8 9 10 11 12
error: `let` expressions are not supported here --> src/main.rs:2:19 | 2 | letvalueA = (letvalueB = 6); | ^^^^^^^^^^^^^^ | = note: only supported directly in conditions of `if` and `while` expressions note: `let`s wrapped in parentheses are not supported in a context with letchains --> src/main.rs:2:19 | 2 | letvalueA = (letvalueB = 6); | ^^^^^^^^^^^^^^
mismatched types
类型不匹配
期望是 i32, 但发现是空元组
1 2 3 4 5 6 7 8
fnmain() { letx = plus_value(5); println!("The value of x {}", x); }
--> src/main.rs:6:30 | 6 | fnplus_value(value: i32) ->i32 { | ---------- ^^^ expected `i32`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression 7 | value + 5; | - help: remove this semicolon ^^^^^^^^^^^^^^
类型不匹配
期待是布尔值,但发现是整型
1 2 3 4 5 6 7 8
fnmain() { letnumber = 3; if number { // solution: if number == 3 { println!("continue was true"); } else { println!("continue was false"); } }
--> src/main.rs:4:28 | 2 | letstring1 = String::from("Hello"); | ------- move occurs because `string1` has type `String`, which does not implement the `Copy` trait 发生移动,是因为类型String不具有Copy特性 3 | letstring2 = string1; | ------- value moved here 4 | println!("string1 {}", string1); | ^^^^^^^ value borrowed here after move value移动后这里又再次使用 | = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace formoreinfo)
--> src/main.rs:13:33 | 10 | lets1 = String::from("Hello, "); | -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait 11 | lets2 = String::from("world!"); 12 | lets3 = s1 + &s2; /// 这里的s1借出去了,后s1无法使用 | -- value moved here 13 | println!("s3 {} s1 {}", s3, s1); | ^^ value borrowed here after move | = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace formoreinfo)
Rust所有权三原则:
Each value in Rust has a variable that’s called its owner. (Rust中每一个变量都有一个所有者)
There can only be one owner at a time.(在任一时刻,所有者有且仅有一个)
When the owner goes out of scope, the value will be dropped.(当所有者离开其作用域后,它所拥有的数据会被释放)
cannot borrow s as mutable more than once at a time
--> src/main.rs:6:14 | 4 | letr1 = &s; | -- immutable borrow occurs here 5 | letr2 = &s; 6 | letr3 = &mut s; | ^^^^^^ mutable borrow occurs here 7 | 8 | println!("r1 {}", r1); | -- immutable borrow later used here
1 2 3 4 5 6
fnmain() { letmut v = vec![1, 2, 3, 4, 5]; letfirst = &v[0]; v.push(6); println!("The first element is: {}", first); // solution:This line of code moves to the previous line }
--> src/main.rs:4:5 | 3 | letfirst = &v[0]; | - immutable borrow occurs here 4 | v.push(6); | ^^^^^^^^^ mutable borrow occurs here 5 | println!("The first element is: {}", first); // solution:This line of code moves to the previous line | ----- immutable borrow later used here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
fnmain() { letmut s = String::from("Hello World!"); letworld = first_word(&s); s.clear(); println!("s {} world {}", s, world); }
fnfirst_word(s: &String) -> &str { letbytes = s.as_bytes(); for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[0..i]; } } return &s[..]; }
--> src/main.rs:6:16 | 6 | fndangle() -> &String { | ^ expected named lifetime parameter | = help: this function'sreturntypecontains a borrowed value, but there is no value forit to be borrowed from help: consider using the `'static` lifetime | 6 | fndangle() -> &'staticString { | ~~~~~~~~
on-exhaustive patterns: &None not covered
详尽的模式:&None 未涵盖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
fnplus_one(x: &Option<i32>) ->Option<i32> { match x { Some(i) => Some(i + 1), } }
fnmain() { leta: i32 = 1; letb: Option<i32> = Some(5); //let c = a + b; /// 错误 //println!("{}", c);
--> src/main.rs:2:11 | 2 | match x { | ^ pattern `&None` not covered | note: `Option<i32>` defined here --> /Users/fliter/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:522:5 | 518 | / pubenumOption<T> { 519 | | /// No value. 520 | | #[lang = "None"] 521 | | #[stable(feature = "rust1", since = "1.0.0")] 522 | | None, | | ^^^^ not covered ... | 526 | | Some(#[stable(feature = "rust1", since = "1.0.0")] T), 527 | | } | |_- = note: the matched value is of type `&Option<i32>` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | 3 ~ Some(i) => Some(i + 1), 4 ~ &None => todo!(), |