Open
Description
As a Rust newbie, no where in "the book" did i learn
- we can make say an immutable vector, mutable via re-assignement
- Likewise we can declare a function that makes vector mutable simply by resassigning like fn name(mut variable:variabletype), every other example takes reference via (variable: &mut variabletype) but not former method
fn main() {
let vec0 = Vec::new(); //create an immutable vector
let vec1 = test(vec0); //vec1 is now mutable
}
fn test(mut vec: Vec<i32>) -> Vec<i32> //the vector here becomes mutable
{
vec.push(2);
vec
}
Update ownershtip chapter
(1) by explaining that mutability can be changed by reassignment, however mutability cannot be changed via references
(2) explain the difference between fn name(mut variable:variabletype) and fn name(variable: &mut variabletype)
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
sircodemane commentedon Mar 8, 2023
I came here to make this same suggestion. I encountered this issue while doing the Rust course offered via JetBrains Academy plugin, which is essentially an interactive version of the rust book. One of the quizzes requires a "mutable move" (?) to solve it and I gave up and just peeked the solution to figure it out.
I looked in the actual rust book and was shocked that I could not find a single explanation of this, and looking up an explanation was very difficult because I often just found people re-iterating the same generic information about references and mutable references that are already in the book.
I know this probably seems like such a simple thing to a typical Rust user, but I spent hours just trying to find anything that could help me demystify what is happening here. Now that I understand it, I find it pretty straight forward, but it took way more effort than I expected to get to this understanding.
chriskrycho commentedon Apr 25, 2024
I can totally see how that ended up being a point of confusion. I have flagged it as a think we might want to tackle as we work on the revisions for the 2024 Edition update of the book.
For other folks who come along, the gist is: the owner of a piece of data can always choose for it to be mutable or not. When you do this (playground)…
…you are moving ownership to
mutable_name
(and you can no longer accessname
). The same thing happens when you pass an item to a function by “move”: now it owns the item, and so it can choose whether to make it mutable or immutable.As @sircodemane notes: it becomes obvious, because it “just” falls out of the way ownership works… but I can totally see how it is not obvious when you are first learning that! I think we can probably work in some text, and maybe a small example, that will make it clearer.
chriskrycho commentedon Mar 21, 2025
Just an administrative note here: I continue to agree that this would be a good thing to solve somewhere and somehow, but I have not found a good way to make that change yet, so I’m pulling this out of the work for the next print edition. If we can come up with a good place to put it, I’m still happy to get it integrated into the web edition, but on a rather longer time frame.