Rust Programming Basics

Rust Basics

Variables

Immutable vs Mutable

In Rust, a variable is immutable, which means you are not allowed to change the data once the variable has data.

#[test]
fn test_variable() {
    let name = "Ardityo Mulawarman";
    println!("Hello {}", name);
    
    name = "Ardi"; // This line will cause a compile-time error because `name` is immutable
    println!("Hello {}", name);
}

Output

Compiling learn-rust v0.1.0 (/Users/ardityo.mulawarman/Developments/rust/learn-rust)
error[E0384]: cannot assign twice to immutable variable `name`
  --> src/main.rs:17:5
   |
14 |     let name = "Ardityo Mulawarman";
   |         ---- first assignment to `name`
...
17 |     name = "Ardi"; // This line will cause a compile-time error because `name` is immutable
   |     ^^^^^^^^^^^^^ cannot assign twice to immutable variable
   |
help: consider making this binding mutable
   |
14 |     let mut name = "Ardityo Mulawarman";
   |         +++

For more information about this error, try `rustc --explain E0384`.
error: could not compile `learn-rust` (bin "learn-rust" test) due to 1 previous error

To change the data, do this instead:

#[test]
fn test_variable() {
    let mut name = "Ardityo Mulawarman";
    println!("Hello {}", name);
    
    name = "Ardi"; 
    println!("Hello {}", name);
}

Output

Compiling learn-rust v0.1.0 (/Users/ardityo.mulawarman/Developments/rust/learn-rust)
 Finished `test` profile [unoptimized + debuginfo] target(s) in 0.09s
  Running unittests src/main.rs (target/debug/deps/learn_rust-71cb9aac0abdb009)

running 1 test
test test_variable ... ok

successes:

---- test_variable stdout ----
Hello Ardityo Mulawarman
Hello Ardi

successes:
    test_variable

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s

Using the mut will allow you to change the data in a variable.

Static Typing

In Rust, basically when we define a variable with a specific data type, we are unable to change the data type (e.g., from string to int) for that variable.

fn static_typing() {
    let mut name = "Ardityo Mulawarman";
    println!("Hello {}", name);

    name = 13; // This line will cause a compile-time error because `name` is of type `&str`
    println!("Hello {}", name);
}

Output

   Compiling learn-rust v0.1.0 (/Users/ardityo.mulawarman/Developments/rust/learn-rust)
error[E0308]: mismatched types
  --> src/main.rs:25:12
   |
22 |     let mut name = "Ardityo Mulawarman";
   |                    -------------------- expected due to this value
...
25 |     name = 13; // This line will cause a compile-time error because `name` is of type `&str`
   |            ^^ expected `&str`, found integer

error[E0384]: cannot assign twice to immutable variable `name`
  --> src/main.rs:17:5
   |
14 |     let name = "Ardityo Mulawarman";
   |         ---- first assignment to `name`
...
17 |     name = "Ardi"; // This line will cause a compile-time error because `name` is immutable
   |     ^^^^^^^^^^^^^ cannot assign twice to immutable variable
   |
help: consider making this binding mutable
   |
14 |     let mut name = "Ardityo Mulawarman";
   |         +++

Some errors have detailed explanations: E0308, E0384.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `learn-rust` (bin "learn-rust" test) due to 2 previous errors

Data Types

Scalar Type

  • Integer
  • Float
  • Boolean
  • Char

Compound Type

  • Tuple
  • Array