Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

if let Some(x) = x

Có thể bạn sẽ gặp pattern này nhiều khi đọc code Rust. Nếu giá trị của xSome thì sẽ destruct giá trị đó bỏ vào biến x nằm trong scope của if.

#![allow(unused)]
fn main() {
fn get_data() -> Option<String> {
    Some("ok".to_string())
}

if let Some(data) = get_data() {
    println!("data = {}", data);
} else {
    println!("no data");
}
}