google / comprehensive-rust

This is the Rust course used by the Android team at Google. It provides you the material to quickly teach Rust.

Home Page:https://google.github.io/comprehensive-rust/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about "Speaker Notes" in Destructuring page

sahuang opened this issue · 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.