Rust Day5 流程控制、模式匹配

65 阅读1分钟

流程控制

// for 循环
for it in list {}

for it in list.iter() {}

for (k, v) in list.iter().enumerate()

// if else语法是表达式
let f = false;
let a = if f { 3 } else { 4 }; // a: 4

 // 无限循环
loop { 
    dosomething
}

模式匹配

match

match类似于switch,比switch功能更强大,处理匹配到的多种模式

match any {
    condition1 => {},
    ...,
    
    // _ => xx 类似于default作用
}

if let

只匹配一种模式的时候,用 if let 更方便

if let condition = {
    dosomething
}

while let

while let condition = {
    dosomething
}