Example solution does not work locally

//Problem 2: Correct the code below
// Solution 

 fn main(){
    let x1 = 40;
    let mut x2 = x1;
    x2 = x1-2;    
    println!("x1 is: {} and x2 is: {}", x1,x2); 
 }

My rustc output:

warning: value assigned to `x2` is never read
 --> ./problem_2.rs:5:13
  |
5 |     let mut x2 = x1;
  |             ^^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

warning: 1 warning emitted

(Playground)

I also ran the code in playground and got this:

   Compiling playground v0.0.1 (/playground)
warning: value assigned to `x2` is never read
 --> src/main.rs:5:9
  |
5 |     let x2 = x1;
  |         ^^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

error[E0384]: cannot assign twice to immutable variable `x2`
 --> src/main.rs:6:5
  |
5 |     let x2 = x1;
  |         --
  |         |
  |         first assignment to `x2`
  |         help: consider making this binding mutable: `mut x2`
6 |     x2 = x1-2;    // do not change this
  |     ^^^^^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted

That error message is from code that's missing mut.

The warning you're seeing indicates that the value assigned to x2 (x2 = x1;) is never actually used, because you immediately overwrite it with a new value (x2 = x1 - 2;). While this doesn't cause an error, it is unnecessary and can be cleaned up.

try it:

let x1 = 40;
let x2 = x1 - 2;
println!("x1 is: {} and x2 is: {}", x1, x2);
1 Like

This will not compiles:

    let x2 = x1; // x2 has been defined as immutable
    x2 = x1-2;  // x2 cannot be changed again

This compiles:

    let mut x2 = x1; // define x2 as mutable
    x2 = x1-2;  // so it can be changed again

he has x2 as mut

You can compile and execute the code by adding a line which uses the mut variable x2. The compiler does not consider the original code to use x2 despite the later lines.

fn main(){
let x1 = 40;
let mut x2 = x1;
let _x3 = x2; //this additional line uses the variable
x2 = x1-2;
println!("x1 is: {} and x2 is: {}", x1,x2);
}

Here's the explanation from "The Rust Programming Language"

If you create a variable but don’t use it anywhere, Rust will usually issue a warning because an unused variable could be a bug. However, sometimes it’s useful to be able to create a variable you won’t use yet, such as when you’re prototyping or just starting a project. In this situation, you can tell Rust not to warn you about the unused variable by starting the name of the variable with an underscore.

Ignore unused variable

Another solution is to use the x2 variable to set the x2 variable.
x2 = x2-x2;
This sets x2 by references x2 and so the compiler identifies a use of the x2 variable.

1 Like