Lädt...


🔧 Variables, Shadowing, and Constants in Rust


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Understanding Variables in Rust

Rust is a statically and strongly typed language. This means that the compiler must know the type of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it. In cases when many types are possible, we must add a type annotation.

In this article, we will learn about the following:

  • variables and mutability
  • shadowing
  • constants

If you prefer a video version

Variables and mutability

Let's start with understanding variables and mutability.

Let's declare a variable and print it:

fn main() {
    let x = 5;
    println!("x is {}", x);
}

Here, we are using string interpolation to print the value of x. The curly braces are placeholders for the value of x. The value of x is then passed as an argument to println!().

You should see the following output:

Variables in Rust - Rust programming tutorial

This is an implicit declaration of type, but at compile time, Rust infers the type of x to be i32. This is because we assigned the value 3 to x, and integer literals default to i32 unless otherwise specified.

If you want to declare the variable type explicitly, you can use a type annotation, which is the colon (:) followed by a type name.

Let's try something that seems we can do but we actually can't:

fn main() {
    let x = 5;
    println!("x is {}", x);

    x = 6;
    println!("x is {}", x);
}

In Rust, variables are immutable by default. This means that once we assign a value to a variable, we can't change it. If we try to change the value of a variable, we get an error:

Variables in Rust - Rust programming tutorial

the mut keyword

If we want to change a variable's value, we must declare it as mutable. We do this by adding the mut keyword before the variable name, as shown here:

fn main() {
    let mut x = 5;
    println!("x is {}", x);

    x = 6;
    println!("x is {}", x);
}

Now we can change the value of x without getting an error.

Shadowing

Now, let's see the concept of shadowing.

We can change the value of an immutable variable by shadowing it. Shadowing is when we declare a new variable with the same name as a previous variable. The new variable shadows the previous variable, and we can assign a new value to the new variable while the old variable remains unchanged.

fn main() {
    let x = 5;
    println!("x is {}", x);

    let x = 6;
    println!("x is {}", x);
}

You might think that we would get an error here, but we don't. This is because we are declaring a new variable, not changing the value of the old one. The old variable is still there, but the new variable shadows it. We can't use the old variable anymore, but we can use the new one.

Variables in Rust - Rust programming tutorial

I can even use the previous variable's value to initialize the new variable:

fn main() {
    let x = 5;
    println!("x is {}", x);

    let x = x + 2;
    println!("x is {}", x);
}

Variables in Rust - Rust programming tutorial

Name shadowing in a different scope

Name shadowing is when we declare a new variable with the same name as a previous variable. The new variable shadows the previous variable, and we can assign a new value to the new variable while the old variable remains unchanged.

Let's see an example:

fn main() {
    let x = 5;
    println!("x is {}", x);

    {
        let x = 8;
        println!("x is {}", x);
    }

    let x = x + 2;
    println!("x is {}", x);
}

Variables in Rust - Rust programming tutorial

Changing the type

When you use shadowing, you can change the type of a variable. This is because you are declaring a new variable, not changing the value of the old one.

You can use it to change the type of a variable:

fn main() {
    let x = 5;
    println!("x is {}", x);

    let x = "hello world!";
    println!("x is {}", x);
}

Variables in Rust - Rust programming tutorial

But you can't change the type of a variable without shadowing it:

fn main() {
    let mut x = 5;
    println!("x is {}", x);

    x = "hello world!";
    println!("x is {}", x);
}

We will get an error.

Variables in Rust - Rust programming tutorial

This means that if we use shadowing, we can change the type, but if a variable is mutable, we can't change the type without shadowing it.

Constants Example

Constants are variables that are immutable and have a fixed value.

They are declared using the const` keyword. They must be annotated with a type, and they can only be set to a constant expression, not the result of a function call or any other value that could only be computed at runtime.

rust
fn main() {
const MAX_LEVEL:i32 = 100_000;
println!("The maximum level is: {}", MAX_LEVEL);
}

Recapt for constants:

  • you must declare the type of the value
  • you must assign a value to the constant on the declaration
  • you cannot redefine or mutate a constant (shadowing doesn't work, can't use mut keyword, can't reassign)

This ends the article.

I hope you enjoyed it and learned something new. If you have any questions, feel free to leave a comment below.

If you prefer a video version

ALL the links here: https://francescociulla.com

...

🔧 Variables, Shadowing, and Constants in Rust


📈 72.42 Punkte
🔧 Programmierung

📰 Rust Basics Series #2: Using Variables and Constants in Rust Programs


📈 55.8 Punkte
🐧 Unix Server

🔧 Variables and Constants: Declaration and Usage


📈 39.57 Punkte
🔧 Programmierung

🔧 Swift 101: Understanding Types, Variables, and Constants


📈 37.94 Punkte
🔧 Programmierung

🔧 Mastering Go: Guide to Type Declarations, Variables, and Constants


📈 37.94 Punkte
🔧 Programmierung

🔧 Static Variables, Constants, and Methods


📈 37.94 Punkte
🔧 Programmierung

🔧 Variables, Constants, Data Types, and Namespaces in C++


📈 37.94 Punkte
🔧 Programmierung

🔧 Task 2 - Constants and variables


📈 37.94 Punkte
🔧 Programmierung

🔧 PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES


📈 37.94 Punkte
🔧 Programmierung

🔧 What are Variables and Constants in Go? Explained With Examples


📈 37.94 Punkte
🔧 Programmierung

🔧 10/7/24 - Day 2 - Data types,variables,constants


📈 36.31 Punkte
🔧 Programmierung

🔧 July 10, 2024 Python | DataTypes, Variables, Constants


📈 36.31 Punkte
🔧 Programmierung

🔧 Mutability and Shadowing in Rust


📈 36.11 Punkte
🔧 Programmierung

🔧 Rust tutorials for Python DEV: Variables and Mutability in Rust


📈 33.44 Punkte
🔧 Programmierung

📰 Linux 6.11: Mehr Rust, Torvalds ergänzt "Runtime Constants"


📈 31.29 Punkte
📰 IT Nachrichten

🔧 Refactoring instance variables to local variables in Rails controllers


📈 27.88 Punkte
🔧 Programmierung

🍏 PCalc for iOS and Mac enhanced with new capabilities for functions, conversions, and constants


📈 25.63 Punkte
🍏 iOS / Mac OS

🔧 Constants in JS and what do Plato and Aristotle have to do with it


📈 25.63 Punkte
🔧 Programmierung

⚠️ [shellcode] Linux/x86 - Disable Shadowing Shellcode (42 bytes)


📈 25.55 Punkte
⚠️ PoC

⚠️ #0daytoday #Linux/x86 - Disable Shadowing Shellcode (42 bytes) [shellcode #0day #Exploit]


📈 25.55 Punkte
⚠️ PoC

📰 A Russian Satellite Appears To Be Shadowing an American Spy Satellite


📈 25.55 Punkte
📰 IT Security Nachrichten

📰 Domain shadowing becoming more popular among cybercriminals


📈 25.55 Punkte
📰 IT Security Nachrichten

📰 Domain Shadowing: Heimlicher Einsatz von DNS Hijacking


📈 25.55 Punkte
📰 IT Security Nachrichten

📰 Domain Shadowing: Heimliche Nutzung der DNS-Kompromittierung durch Angreifer


📈 25.55 Punkte
📰 IT Security Nachrichten

🕵️ So funktioniert Domain Shadowing


📈 25.55 Punkte
🕵️ Hacking

📰 Domain Shadowing: schwer zu entdeckende Cyberattacke


📈 25.55 Punkte
📰 IT Security Nachrichten

🔧 What is Variable Shadowing in JavaScript?


📈 25.55 Punkte
🔧 Programmierung

🔧 Learning Go - Blocos, Shadowing e Estruturas de controle


📈 25.55 Punkte
🔧 Programmierung

🔧 What is Variable Shadowing in Javascript


📈 25.55 Punkte
🔧 Programmierung

🔧 What is Illegal Shadowing


📈 25.55 Punkte
🔧 Programmierung

🕵️ CVE-2024-36073 | Netwrix CoSoSys Endpoint Protector/CoSoSys Unify Shadowing Privilege Escalation


📈 25.55 Punkte
🕵️ Sicherheitslücken

🔧 Shadowing in JavaScript


📈 25.55 Punkte
🔧 Programmierung

🔧 Variables and Primitive Data Types in Rust


📈 24.5 Punkte
🔧 Programmierung

🔧 C++ vs. Rust: A Comparative Guide to Syntax, Variables, and Control Flow


📈 24.5 Punkte
🔧 Programmierung

matomo