April 2026
Intermediate
631 pages
16h 20m
English
A code block is a section of code enclosed in curly braces {}. You use code blocks in many contexts, including functions, loops, conditionals, and more. They group together multiple statements and expressions, and in Rust, the last expression in a code block is automatically returned as the value of that block.
We mentioned code blocks earlier in Section 2.1.3 when discussing the scope of a variable. Now, consider the code shown in Listing 2.9.
fn main() { let full_name = { let first_name = "John"; let last_name = "Archer"; format!("{first_name} {last_name}") };}
Listing 2.9 An Example of a Code Block
The format! macro is used for String formatting. The placeholders inside the double quotes will be filled with the respective ...
Read now
Unlock full access