How to use block as expression in Rust

2 Answers

0 votes
fn main() {
    let x = 7u32;

    let y = {
        2 * x
    };

    println!("x = {:?}", x);
    println!("y = {:?}", y);
}





/*
run:

x = 7
y = 14

*/

 



answered May 3, 2023 by avibootz
0 votes
fn main() {
    let x = 7u32;

    let y = {
        let x_squared = x * x;
        let x_cube = x_squared * x;

        x_cube + x_squared
    };

    println!("x = {:?}", x);
    println!("y = {:?}", y);
}





/*
run:

x = 7
y = 392

*/

 



answered May 3, 2023 by avibootz
...