diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 306d2cd102f..055129b35df 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -75,11 +75,13 @@ into a variable called `op_string` while simultaneously requiring the inner String to be moved into a variable called `s`. +``` let x = Some("s".to_string()); match x { op_string @ Some(s) => ... None => ... } +``` See also Error 303. "##, @@ -90,10 +92,12 @@ referenced in the pattern guard code. Doing so however would prevent the name from being available in the body of the match arm. Consider the following: +``` match Some("hi".to_string()) { Some(s) if s.len() == 0 => // use s. ... } +``` The variable `s` has type String, and its use in the guard is as a variable of type String. The guard code effectively executes in a separate scope to the body @@ -102,11 +106,13 @@ innocuous, the problem is most clear when considering functions that take their argument by value. +``` match Some("hi".to_string()) { Some(s) if { drop(s); false } => (), Some(s) => // use s. ... } +``` The value would be dropped in the guard then become unavailable not only in the body of that arm but also in all subsequent arms! The solution is to bind by @@ -218,6 +224,7 @@ struct X { x: (), } match was succesful. If the match is irrefutable (when it cannot fail to match), use a regular `let`-binding instead. For instance: +``` struct Irrefutable(i32); let irr = Irrefutable(0); @@ -230,6 +237,7 @@ struct X { x: (), } // Try this instead: let Irrefutable(x) = irr; foo(x); +``` "##, E0165: r##" @@ -237,6 +245,7 @@ struct X { x: (), } match was succesful. If the match is irrefutable (when it cannot fail to match), use a regular `let`-binding inside a `loop` instead. For instance: +``` struct Irrefutable(i32); let irr = Irrefutable(0); @@ -250,6 +259,7 @@ struct X { x: (), } let Irrefutable(x) = irr; ... } +``` "##, E0170: r##" @@ -304,6 +314,7 @@ enum Method { GET, POST } loop variable, consider using a `match` or `if let` inside the loop body. For instance: +``` // This fails because `None` is not covered. for Some(x) in xs { ... @@ -323,6 +334,7 @@ enum Method { GET, POST } ... } } +``` "##, E0301: r##" @@ -332,11 +344,13 @@ enum Method { GET, POST } exhaustive. For instance, the following would not match any arm if mutable borrows were allowed: +``` match Some(()) { None => { }, option if option.take().is_none() => { /* impossible, option is `Some` */ }, Some(_) => { } // When the previous match failed, the option became `None`. } +``` "##, E0302: r##" @@ -346,11 +360,13 @@ enum Method { GET, POST } exhaustive. For instance, the following would not match any arm if assignments were allowed: +``` match Some(()) { None => { }, option if { option = None; false } { }, Some(_) => { } // When the previous match failed, the option became `None`. } +``` "##, E0303: r##" @@ -358,9 +374,10 @@ enum Method { GET, POST } Updates to the borrow checker in a future version of Rust may remove this restriction, but for now patterns must be rewritten without sub-bindings. -// Before. -match Some("hi".to_string()) { - ref op_string_ref @ Some(ref s) => ... +``` +// Code like this... +match Some(5) { + ref op_num @ Some(num) => ... None => ... } @@ -372,6 +389,7 @@ enum Method { GET, POST } } None => ... } +``` The `op_string_ref` binding has type &Option<&String> in both cases.