Duration
Option<T>
์ ์ผ๋ถ ์ฌ์ฉ๋ฒ์ ์ด๋ฏธ ์ดํด๋ดค์ต๋๋ค. โTโ ํ์
์ ๊ฐ์ ์ ์ฅํ๊ฑฐ๋ ์๋ฌด๊ฒ๋ ์ ์ฅํ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด String::find
๋ Option<usize>
๋ฅผ ๋ฐํํฉ๋๋ค.
fn main() { let name = "Lรถwe ่่ Lรฉopard Gepardi"; let mut position: Option<usize> = name.find('รฉ'); println!("find์ ๋ฐํ๊ฐ {position:?}"); assert_eq!(position.unwrap(), 14); position = name.find('Z'); println!("find์ ๋ฐํ๊ฐ {position:?}"); assert_eq!(position.expect("๋ฌธ์๋ฅผ ์ฐพ์ ์ ์์"), 0); }
This slide should take about 10 minutes.
Option
is widely used, not just in the standard library.unwrap
์Option
์ ๊ฐ์ ๋ฐํํ๊ฑฐ๋ ํจ๋์ ๋ฐํํฉ๋๋ค.expect
๋ ๋น์ทํ์ง๋ง ์ค๋ฅ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.- None ๋ฐ์ ์ ํจ๋ ์ํ๊ฐ ๋ ์ ์์ง๋ง โ์ค์โ๋ก None์ ์ฒดํฌํ๋ ๊ฒ์ ์์ ์๋ ์์ต๋๋ค.
- ๋ฌด์ธ๊ฐ๋ฅผ ํจ๊ป ํดํนํ ๋ ๋ชจ๋ ๊ณณ์์
unwrap
/expect
๋ฅผ ์คํํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด์ง๋ง ํ๋ก๋์ ์ฝ๋๋ ์ผ๋ฐ์ ์ผ๋ก ๋ ๋์ ๋ฐฉ์์ผ๋กNone
์ ์ฒ๋ฆฌํฉ๋๋ค.
- ํ์ ์ต์ ํ๋
Option<T>
๊ฐ ๋ฉ๋ชจ๋ฆฌ์์T
์ ํฌ๊ธฐ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.