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