当前位置: 首页>后端>正文

与c++比较学习rust3-5:控制流

rust 的文章在 控制流

这一章亮点
match 不用break 比 c++的switch好不知道多少倍

if表达式

if
fu abc(){
    let a = 1;
    if  a == 1 {

    } else if a == 2 {

    }else {

    }
}

在let语句中使用

let a = if condi {5} else {4};  //if else 必须是相同的数据类型
match
let state = match state_code {
      "MH" => {println!("xxx"); "Maharashtra"},
      "KL" => "Kerala",
      "KA" => "Karnadaka",
      "GA" => "Goa",
      _ => "Unknown"
   };
   println!("State name is {}",state);

循环语句

loop
let mut x = 0;
loop {
    x += 1;
}
while
let mut x = 0;
while x <10 {
    x += 1;
}
for
for var in lower_bound..upper_bound{
    //
}
for item in arr{
    //
}

break/continue

与 c++ 一样


https://www.xamrdz.com/backend/3z71937947.html

相关文章: