cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
636 stars 100 forks source link

Chapter 4.3: Access Descriptor Issue + Missing Clarifications on Suggested Bug Fix #223

Open Tinku10 opened 1 week ago

Tinku10 commented 1 week ago

I am completely new to Rust and started going through the book just 2-3 days ago. Sorry if I have incorrectly pointed out anything here.

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch04-03-fixing-ownership-errors.html#fixing-an-unsafe-program-copying-vs-moving-out-of-a-collection

Description of the problem: The diagram tells us that v has lost Read access, which in reality it should not.

Suggested fix: Mark v with R

image

Apart from this, I think the section Fixing an Unsafe Program: Not Enough Permissions does not take into account the previously linked main function for the suggested solutions.

Here is the described main function.

fn stringify_name_with_title(name: &Vec<String>) -> String {
    name.push(String::from("Esq."));`{}`
    let full = name.join(" ");
    full
}

fn main() {
    let name = vec![String::from("Ferris")];
    let first = &name[0];
    stringify_name_with_title(&name);
    println!("{}", first);
}

The first suggested fix mutates the name rending the access of first in main problematic.

fn stringify_name_with_title(name: &mut Vec<String>) -> String {
    name.push(String::from("Esq."));
    let full = name.join(" ");
    full
}

The second solution moves the name causing the same problem.

fn stringify_name_with_title(mut name: Vec<String>) -> String {
    name.push(String::from("Esq."));
    let full = name.join(" ");
    full
}