Skip to content

Giving away ownership can make immutable, mutable #3291

Open
@rohitisinhk

Description

@rohitisinhk

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

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions