MOCKSTACKS
EN
Questions And Answers

More Tutorials




What are mutable and immutable variables in Rust?

Rust defaults variable declaration to immutability, meaning unless you state otherwise, it assumes that what you’re creating should be treated like a constant. To facilitate this, the mut keyword can be used to indicate a variable should be mutable, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created:

Example with error


fn main() {

    // This is an integer without the "mut" keyword, and therefore
    // defaults to immutable. We will get a compiler error if we try
    // to change this.
    let intConst = 5;
    
    // The "mut" keyword means we're "opting in" to mutability here,
    // so "intVar" will act like a traditional variable. We can change it.
    let mut intVar = 5;
          
    intVar = 6;    
    intConst = 6;    // error[E0384]: cannot assign twice to immutable variable `intConst`
    println!("{}", intConst);
    println!("{}", intVar);
}

Output

error[E0384]: cannot assign twice to immutable variable `intConst`


Fix Above example

To fix above example we will need to remove the line where we try to reassign the a value again:

fn main() {

    // This is an integer without the "mut" keyword, and therefore
    // defaults to immutable. We will get a compiler error if we try
    // to change this.
    let intConst = 5;
    
    // The "mut" keyword means we're "opting in" to mutability here,
    // so "intVar" will act like a traditional variable. We can change it.
    let mut intVar = 5;
          
    intVar = 6;    
    //// We removed this line so we don't re-assign the value again after declaration.
    println!("{}", intConst);
    println!("{}", intVar);
}

Output

5
6




Conclusion

In this page (written and validated by ) you learned about What are mutable and immutable variables in Rust? . What's Next? If you are interested in completing Rust tutorial, we encourage you simply to start here: Rust Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.