Basic Blocks and Scopes - Tutorial

⚓ Rust    📅 2026-03-12    👤 surdeus    👁️ 5      

surdeus

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`

2 posts - 2 participants

Read full topic

🏷️ Rust_feed