Blocks
๋ฌ์คํธ์์ ๋ธ๋ก์ ๊ฐ๊ณผ ํ์ ์ ๊ฐ์ต๋๋ค: ๋ธ๋ก์ ํํ์์ด ๊ฐ์ด ๋ฉ๋๋ค.
A block in Rust has a value and a type: the value is the last expression of the block:
fn main() { let x = { let y = 10; println!("y: {y}"); let z = { let w = { 3 + 4 }; println!("w: {w}"); y * w }; println!("z: {z}"); z - y }; println!("x: {x}"); }
ํจ์์๋ ๋์ผํ ๊ท์น์ด ์ ์ฉ๋ฉ๋๋ค: ํจ์๋ฐ๋์ (๋ง์ง๋ง) ๊ฐ์ด ๋ฐํ ๊ฐ์ด ๋ฉ๋๋ค.
The same rule is used for functions: the value of the function body is the return value:
fn double(x: i32) -> i32 { x + x } fn main() { println!("doubled: {}", double(7)); }
์ญ์ฃผ
- ๋ง์ง๋ง ์ค์ ; ์๋ ๋ถ๋ถ์ ๊ตฌ๋ฌธ(statements)์ด ์๋๋ผ ํํ์(expressions)์ด๋ผ๊ณ ํฉ๋๋ค.
- ๋ธ๋ก์์ ๊ตฌ๋ฌธ+ ๋ง์ง๋ง ํํ์ ์ธ๊ฒฝ์ฐ ํด๋น ํจ์๋ฅผ ํํ์์ผ๋ก ๋ด ๋๋ค.