Rust <3>:控制流

if、loop、for、while:

fn main() {
    if 3 > 4 { println!{">"}; }
    else { println!{"<"}; }

    let mut zvar = if 2 > 5 {2} else {5};  // 相当于多目运算,各目数据类型必须完全相同
    while zvar > 0 {
        println!("zvar: {}", zvar);
        zvar -= 1;
    }

    for zelement in [1, 2, 3].iter() {
        println!("{}", zelement);
    }

    loop {
        println!("Hello World!");
        break;
    }
}

match:

struct _IpAddr {
    ip_addr: String,
}

impl _IpAddr {
    fn print(&self) {
        println!("{}", self.ip_addr);
    }
}

fn main() {
    let _ip_addr = _IpAddr {ip_addr: String::from("::1")};
    let _id = 1;
    match _id {
        0 => _ip_addr.print(),
        _ => {
              println!("nothing...");
          },
    }

    println!("Hello, world!");
}

____

posted @ 2017-10-31 21:33  范辉  阅读(243)  评论(0编辑  收藏  举报