Rust tutorials for Python DEV: Variables and Mutability in Rust

Beginner’s Guide to Variables and Mutability in Rust

In Rust, the let keyword is used to declare variables. When you declare a variable with let, you are introducing a new binding (or name) that associates with a value of a particular type.


This content originally appeared on DEV Community and was authored by ahmed elboshi

Beginner's Guide to Variables and Mutability in Rust

In Rust, the let keyword is used to declare variables. When you declare a variable with let, you are introducing a new binding (or name) that associates with a value of a particular type.
Here's a detailed explanation of how to assign variables using let in Rust:

Basic Variable Assignment

To assign a variable in Rust, you use the let keyword followed by the variable name and optionally a type annotation (: type) if you want to explicitly specify the type.

Example:

fn main() {
    // Declare a variable named `x` and assign it the value `5`
    let x = 5;

    // Print the value of `x`
    println!("The value of x is: {}", x);
}

In this example:

  • let x = 5; declares a variable named x and assigns it the value 5.
  • Rust infers the type of x as i32 (a 32-bit signed integer) based on the assigned value.

Explicit Type Annotation

If you want to explicitly specify the type of a variable, you can do so by appending : type after the variable name.

Example:

fn main() {
    // Declare a variable named `y` with type annotation `i64` (a 64-bit signed integer)
    let y: i64 = 100;

    // Print the value of `y`
    println!("The value of y is: {}", y);
}

In this example:

  • let y: i64 = 100; declares a variable named y with the type annotation i64 and assigns it the value 100.

Rebinding Variables with let

In Rust, variables are immutable by default. Once you assign a value to a variable, you cannot reassign it. However, you can rebind a variable by using the let keyword again, which shadows the previous binding.

Example:

fn main() {
    let z = 10;
    println!("The value of z is: {}", z);  // Output: The value of z is: 10

    let z = "hello";
    println!("The new value of z is: {}", z);  // Output: The new value of z is: hello
}

In this example:

  • let z = 10; declares a variable z and assigns it the value 10.
  • Later, let z = "hello"; shadows the previous z variable with a new binding of type &str (a string slice).

mut

In Rust, variables are by default immutable, meaning once you assign a value to a variable, you cannot change it. However, you can explicitly make variables mutable using the mut keyword.
Immutable variables are declared using the let keyword without mut. Once assigned, their value cannot be changed.

Python:

x = 5
x = 10  # This is allowed

Rust:

fn main() {
    let x = 5;
    // x = 10; // Uncommenting this line will cause a compile-time error

    println!("The value of x is: {}", x);
}

Breaking Down the Code

  1. Declare an Immutable Variable:
   let x = 5;

This declares a variable x with the value 5. Rust infers the type of x as i32 (32-bit signed integer) in this case.

  1. Trying to Reassign the Variable:
   // x = 10; // Uncommenting this line will cause a compile-time error

Rust does not allow reassigning x because it's immutable.

Mutable Variables

If you want to change the value of a variable, you need to declare it as mutable using the mut keyword.

Python:

x = 5
x = 10  # This is allowed

Rust:

fn main() {
    let mut y = 5;
    y = 10;  // This is allowed because y is mutable
    println!("The value of y is: {}", y);
}

Breaking Down the Code

  1. Declare a Mutable Variable:
   let mut y = 5;

This declares a mutable variable y with the initial value 5.

  1. Assign a New Value:
   y = 10;

Since y is mutable, you can reassign its value to 10.

Why Use Mutability?

In Rust, immutability by default helps prevent unintended changes to data, which can help catch bugs at compile time. Mutable variables are useful when you need to change the value of a variable after it's initially set.

Conclusion

Understanding variables and mutability in Rust is fundamental to writing safe and efficient code. By default, variables are immutable, and you can make them mutable by using the mut keyword. This ensures that your code is more predictable and less prone to bugs related to unintended changes. Happy coding in Rust!

This tutorial introduces the concept of variables and mutability in Rust, highlighting how Rust's default immutability helps ensure safer and more predictable code.


This content originally appeared on DEV Community and was authored by ahmed elboshi


Print Share Comment Cite Upload Translate Updates
APA

ahmed elboshi | Sciencx (2024-07-14T04:28:27+00:00) Rust tutorials for Python DEV: Variables and Mutability in Rust. Retrieved from https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/

MLA
" » Rust tutorials for Python DEV: Variables and Mutability in Rust." ahmed elboshi | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/
HARVARD
ahmed elboshi | Sciencx Sunday July 14, 2024 » Rust tutorials for Python DEV: Variables and Mutability in Rust., viewed ,<https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/>
VANCOUVER
ahmed elboshi | Sciencx - » Rust tutorials for Python DEV: Variables and Mutability in Rust. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/
CHICAGO
" » Rust tutorials for Python DEV: Variables and Mutability in Rust." ahmed elboshi | Sciencx - Accessed . https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/
IEEE
" » Rust tutorials for Python DEV: Variables and Mutability in Rust." ahmed elboshi | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/. [Accessed: ]
rf:citation
» Rust tutorials for Python DEV: Variables and Mutability in Rust | ahmed elboshi | Sciencx | https://www.scien.cx/2024/07/14/rust-tutorials-for-python-dev-variables-and-mutability-in-rust/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.