Option
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.
Optionis widely used, not just in the standard library.unwrapμOptionμ κ°μ λ°ννκ±°λ ν¨λμ λ°νν©λλ€.expectλ λΉμ·νμ§λ§ μ€λ₯ λ©μμ§κ° νμλ©λλ€.- None λ°μ μ ν¨λ μνκ° λ μ μμ§λ§ βμ€μβλ‘ Noneμ 체ν¬νλ κ²μ μμ μλ μμ΅λλ€.
- 무μΈκ°λ₯Ό ν¨κ» ν΄νΉν λ λͺ¨λ κ³³μμ
unwrap/expectλ₯Ό μ€ννλ κ²μ΄ μΌλ°μ μ΄μ§λ§ νλ‘λμ μ½λλ μΌλ°μ μΌλ‘ λ λμ λ°©μμΌλ‘Noneμ μ²λ¦¬ν©λλ€.
- νμ μ΅μ νλ
Option<T>κ° λ©λͺ¨λ¦¬μμTμ ν¬κΈ°κ° κ°μ κ²½μ°κ° λ§λ€λ κ²μ μλ―Έν©λλ€.