Rust:- Variable Declaration – Mutable ( mut )

Background

I was reviewing a code snippet from StackOverflow.

The variable was declared with the mut keyword.

I wanted to see when the mut keyword is needed.

Let us get with it.

Code

Outline

Main Function

  1. Declare Variables
    • let initial:i32;
    • let mut value:i32;
    • let resultSave:i32;
    • let mut result:i32;
  2. Set Variable Values
    • initial =7;
    • value =3;
  3. Invoke Function
    • result = incrementCorrected(initial, value);
  4. Print Result

Function – Declaration – incrementOriginal

  1. Function Header
    • fn incrementOriginal(result: i32, value: i32 )-> i32
  2. Arguments
    • result
      • Type i32
    • value
      • Type i32
  3. Return
    • result
      • Type i32

 

Function – Declaration – incrementCorrected

  1. Function Header
    • fn incrementCorrected(mut result: i32, value: i32 )-> i32
  2.  Arguments
    • result
      • Type i32 ( mut )
    • value
      • Type i32
  3. Return
    • resultNew
      • Type i32

 

Function – Declaration – incrementRevised

  1. Function Header
    • fn incrementRevised(result: i32, value: i32 )-> i32
  2.  Arguments
    • result
      • Type i32 ( mut )
    • value
      • Type i32
  3. Return
    • result
      • Type i32

 

Code Snippets

Function – incrementOriginal


/*
    Stores result into an existing variable ( fails )
*/
fn incrementOriginal(result: i32, value: i32 )-> i32{

    result = result + value;
    
    return result;
    
}

Function – incrementCorrected


/*
    Stores result into an existing variable ( works )
*/
fn incrementCorrected(mut result: i32, value: i32 )-> i32{
    
    result = result + value;
    
    return result;
    
}

Function – incrementRevised


/*
    Stores result into a new variable ( works)
*/
fn incrementRevised(result: i32, value: i32 )-> i32{

    let resultNew: i32;
    
    resultNew = result + value;
    
    return resultNew;
    
}

Code Source


fn main() {

    /*
        Declare Variables
    */
    let initial:i32;
    let mut value:i32;
    
    let resultSave:i32;
    let mut result:i32;
   
    // set variable values
    initial =7;
    value =3;
    
    //invoke function - incrementCorrected 
    result = incrementCorrected(initial, value);
    
    // Print text to the console
    println!(
                  "Add {1} to {0} yields {2}"
                , initial
                , value
                , result
            );
            
    // set variable values
    value = -2;
    resultSave = result;
    
    //invoke function - incrementRevised
    result = incrementRevised(resultSave, value);  
     
    // Print text to the console
    println!(
                  "Add {1} to {0} yields {2}"
                , resultSave
                , value
                , result
            );     
    
}


/*

    Please comment out this function to get a good compile
    Function only left here as mouth trap
    
    The difference between this function and the successive one 
    is in the definition off the result Variable
    
    a) result: i32
    b) mut result: i32
    
*/


/*
    Stores result into an existing variable ( attempted )
*/
fn incrementOriginal(result: i32, value: i32 )-> i32{

    result = result + value;
    
    return result;
    
}


/*
    Stores result into an existing variable ( works )
*/
fn incrementCorrected(mut result: i32, value: i32 )-> i32{
    
    result = result + value;
    
    return result;
    
}

/*
    Stores result into a new variable ( works)
*/
fn incrementRevised(result: i32, value: i32 )-> i32{

    let resultNew: i32;
    
    resultNew = result + value;
    
    return resultNew;
    
}


 

Error

Compilation Error

error[E0384]: cannot assign to immutable argument

Textual


error[E0384]: cannot assign to immutable argument `result`
  --> main.rs:64:5
   |
62 | fn incrementOriginal(result: i32, value: i32 )-> i32{
   |                      ------ help: consider making this binding mutable: `mut result`
63 |
64 |     result = result + value;
   |     ^^^^^^^^^^^^^^^^^^^^^^^ cannot assign to immutable argument
error: aborting due to previous error
For more information about this error, try `rustc --explain E0384`.

Image

Explanation

Our function is defined as:-


/*
    Stores result into an existing variable ( fails )
*/
fn incrementOriginal(result: i32, value: i32 )-> i32{

    result = result + value;
    
    return result;
    
}

The function adds result and value.

But, fails when it tries to store same in result.

Reason being that result is not defined as mutable.

Revision

Please change

from


fn incrementOriginal(result: i32, value: i32 )-> i32{

to


fn incrementOriginal(mut result: i32, value: i32 )-> i32{

 

Code Sharing

  1. Onlinegdb
    • rustVariableDeclarationMutable
      Link
  2. GitLab
    • rustVariableDeclarationMutable
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s