Basic Blocks and Scopes - Tutorial

fn main() {
    // Blocos e Escopos
    let z = 12;
    let x = {
        let y = 10;
        println!("Y: {y}");
        z - y
    };
    println!("X: {:?}", x);
    
    // Escopos e Shadowing
    let a = 10;
    println!("Antes: {a}");
    {
        let a = "olá";
        let b = "mundo";
        println!("Escopo interno: {a} {b}");
        
        let a = true;
        println!("Sobreposição no escopo interno: {a}");
    }
    println!("Depois: {a}");
    // println!("B: {b}") - Não pode acessar
}

(Playground)

Output:

Y: 10
X: 2
Antes: 10
Escopo interno: olá mundo
Sobreposição no escopo interno: true
Depois: 10

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished `release` profile [optimized] target(s) in 0.72s
     Running `target/release/playground`

This is the third time in a short time frame you shared a (fairly basic) snippet with us. Please note that this is a forum meant for people to discuss and engage in certain aspects of the Rust programming language. It is not a place for storing your personal playground snippets. Please consider using a more suitable storage tool, like GitHub Gist, for such purposes.

4 Likes