if
ํํ์
๋ค๋ฅธ ์ธ์ด์ if
๋ฌธ๊ณผ ๋๊ฐ์ด if
ํํ์์ ์ฌ์ฉํฉ๋๋ค:
fn main() { let x = 10; if x == 0 { println!("zero!"); } else if x < 100 { println!("ํฐ"); } else { println!("๊ฑฐ๋ํ"); } }
๊ฒ๋ค๊ฐ if
๋ ํํ์์ผ๋ก ์ฌ์ฉํ ์๋ ์์ต๋๋ค. ์๋ ์ฝ๋๋ ์์ ๋์ผํฉ๋๋ค:
fn main() { let x = 10; let size = if x < 20 { "์์" } else { "๋ํ" }; println!("์ซ์ ํฌ๊ธฐ: {}", size); }
This slide should take about 4 minutes.
Because if
is an expression and must have a particular type, both of its branch blocks must have the same type. Show what happens if you add ;
after "small"
in the second example.
ํํ์์ 'if'๊ฐ ์ฌ์ฉ๋ ๊ฒฝ์ฐ ๋ค์ ๋ฌธ๊ณผ ๊ตฌ๋ถํ๊ธฐ ์ํด ํํ์์ ;
์ด ์์ด์ผ ํฉ๋๋ค. ์ปดํ์ผ๋ฌ ์ค๋ฅ๋ฅผ ๋ณด๋ ค๋ฉด println!
์์ ;
์ ์ญ์ ํ์ธ์.