Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about "Speaker Notes" in Destructuring page #2023

Closed
sahuang opened this issue Apr 23, 2024 · 2 comments
Closed

Question about "Speaker Notes" in Destructuring page #2023

sahuang opened this issue Apr 23, 2024 · 2 comments

Comments

@sahuang
Copy link

sahuang commented Apr 23, 2024

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

@djmitche
Copy link
Collaborator

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.

@sahuang
Copy link
Author

sahuang commented Apr 24, 2024

Thanks! Noted.

@sahuang sahuang closed this as completed Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants