您现在的位置是:首页 > 编程 > 

【Rust】00

2025-07-24 04:11:33
【Rust】00 【Rust】00-基础语法:流程控制一、概述控制流是编程语言的一个重要概念。程序员通过控制流可以控制哪些代码要执行。在Rust中,最常见的两种控制流结构是if表达式和循环。二、if 表达式1、语法格式 这里的 condition 必须是 bool 类型 代码语言:javascript代码运行次数:0运行复制if condition { // code to exec

【Rust】00

【Rust】00-基础语法:流程控制一、概述

控制流是编程语言的一个重要概念。程序员通过控制流可以控制哪些代码要执行。在Rust中,最常见的两种控制流结构是if表达式和循环。

二、if 表达式

1、语法格式

这里的 condition 必须是 bool 类型

代码语言:javascript代码运行次数:0运行复制
if condition {
    // code to execute if the condition is true
} else {
    // code to execute if the condition is false
}

2、多个

代码语言:javascript代码运行次数:0运行复制
if condition1 {
    // code to execute if condition1 is true
} else if condition2 {
    // code to execute if condition1 is false and condition2 is true
} else {
    // code to execute if both conditi are false
}

、获取表达式的值

正如这一小节的标题所说,if其实是一个表达式,具有返回值。 需要注意的是,if分支和else分支的返回值必须是同一类型

代码语言:javascript代码运行次数:0运行复制
fn main() {
    let temperature = 20;

    let weather = if temperature >= 25 {
        "hot"
    } else {
        "cool"
    };

    println!("The weather today is {}.", weather);
}
三、循环

Rust中提供了三种循环方式,loopwhilefor

1、loop:无限循环,可跳出

无限循环

loop关键字会创建一个无限循环

代码语言:javascript代码运行次数:0运行复制
loop {
    // code to execute repeatedly
}
跳出循环

想要从循环中跳出,需要配合break关键词使用,下面的代码也展示了 continue 的用法!

代码语言:javascript代码运行次数:0运行复制
let mut counter = 0;

loop {
    counter += 1;
    if counter < 5 {
        continue;
    }
    println!("Hello, world!");
    if counter >= 5 {
        break;
    }
}
返回值
代码语言:javascript代码运行次数:0运行复制
fn main() {
    let target = 10;
    let mut sum = 0;
    let mut counter = 1;

    let result = loop {
        sum += counter;

        if sum >= target {
            break counter; // The value of counter will be returned from the loop as a result
        }

        counter += 1;
    };

    println!("The first number whose sum of all previous numbers is greater than or equal to {} is {}.", target, result);
}

2、while:条件循环,可跳出

代码语言:javascript代码运行次数:0运行复制
while condition {
    // code to execute while the condition is true
}

、for:常用于访问集合

访问集合:while 示例
代码语言:javascript代码运行次数:0运行复制
fn main() {
    let numbers = [1, 2, , 4, 5];
    let mut index = 0;

    while index < numbers.len() {
        println!("The value is: {}", numbers[index]);
        index += 1;
    }
}
访问集合:for 示例
代码语言:javascript代码运行次数:0运行复制
fn main() {
    let numbers = [1, 2, , 4, 5];

    for number in numbers {
        println!("The value is: {}", number);
    }
}
对一个 range 进行循环

这里的1..=表示[1,]这个区间的整数。如果是左闭右开,要写成1..

代码语言:javascript代码运行次数:0运行复制
fn main() {
    for x in 1..= {
        println!("x: {}", x);
    }
}

4、labels:给循环加标签

三种循环都支持!

当循环存在嵌套关系时,breakcontinue只会对最内层的循环生效。但是有时候我们希望可以对外层的循环做break或者continue,这时该怎么办?幸运的是,Rust 可以给循环加上标签,从而breakcontinue都可以直接操作标签。

代码语言:javascript代码运行次数:0运行复制
fn main() {
    let x = 1;

    'outer: loop {
        let mut y = 1;

        'inner: loop {
            if y ==  {
                y += 1;
                continue 'inner; // Skips to the next iteration of the 'inner loop
            }

            println!("x: {}, y: {}", x, y);

            y += 1;

            if y > 5 {
                break 'outer; // Breaks out of the 'inner loop
            }
        }
    }
}

带返回值

代码语言:javascript代码运行次数:0运行复制
fn main() {
    let x = 1;

    let z = 'outer: loop {
        let mut y = 1;

        'inner: loop {
            if y ==  {
                y += 1;
                continue 'inner; // Skips to the next iteration of the 'inner loop
            }

            println!("x: {}, y: {}", x, y);

            y += 1;

            if y > 5 {
                break 'outer y; // Breaks out of the 'inner loop
            }
        }
    };
    println!("z: {}", z);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-04-0,如有侵权请联系 cloudcommunity@tencent 删除counter基础集合语法rust

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1195513.html

相关标签:无
上传时间: 2025-07-23 06:29:20

上一篇:【Rust】004

下一篇:【Rust】002

留言与评论(共有 8 条评论)
本站网友 小寒资料集
26分钟前 发表
{}"
本站网友 青岛店铺转让
28分钟前 发表
4
本站网友 南充租房
1分钟前 发表
{}"
本站网友 乌鸡白凤丸的功效与作用
27分钟前 发表
loop { if y == { y += 1; continue 'inner; // Skips to the next iteration of the 'inner loop } println!("x
本站网友 松原租房
14分钟前 发表
{}
本站网友 hrd是什么意思
2分钟前 发表
{}
本站网友 大连大学是几本
26分钟前 发表
x