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

Chapter 5.3 - Reads and Writes with &self and &mut self : rect do not lose Read permission #202

Open
dhmoclex opened this issue Aug 2, 2024 · 0 comments

Comments

@dhmoclex
Copy link

dhmoclex commented Aug 2, 2024

URL to the section(s) of the book with this problem:
https://rust-book.cs.brown.edu/ch05-03-method-syntax.html#reads-and-writes-with-self-and-mut-self

Description of the problem:
On the last memory diagram (Added the mut keyword to the let-binding), rect do not lose Read permission.

For instance this program is valid :

struct Rectangle {
    width: u32,
    height: u32,
}
impl Rectangle {
    fn set_width(&mut self, width: u32) {
        self.width = width;
    }
}
fn main() {
    // Added the mut keyword to the let-binding
    let mut rect = Rectangle {
        width: 0,
        height: 0,
    };
    rect.set_width(1); // this is now ok

    let rect_ref = ▭
    println!("{} {}", rect.width, rect.height);
    println!("{} {}", rect_ref.width, rect_ref.height);
}

Suggested fix:
Do not strike Read permission for rect, rect.width and rect.height.

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

1 participant