google/comprehensive-rust

Question about "Speaker Notes" in Destructuring page

sahuang opened this issue · 2 comments

The "Speaker Notes" in Destructuring Enums mentioned that:

Save the result of divide_in_two in the result variable and match it in a loop. That won’t compile because msg is consumed when matched. To fix it, match &result instead of result. That will make msg a reference so it won’t be consumed.

However, I tried this and it compiles successfully even without the &. Is it because new rustc version somehow fixed this issue?

image

I think the missing bit is "in a loop"

fn main() {
    let n = 100;
    let result = divide_in_two(n);
    loop {
        match result {
            Result::Ok(half) => println!("{n} divided in two is {half}"),
            Result::Err(msg) => println!("sorry, an error happened: {msg}"),
        }
    }
}

BTW, please try to use text to represent text, rather than images -- in this case I couldn't copy/paste from your example because it's an image.

Thanks! Noted.