【Rust】003-基礎(chǔ)語(yǔ)法:流程控制
一、概述
控制流是編程語(yǔ)言的一個(gè)重要概念。程序員通過控制流可以控制哪些代碼要執(zhí)行。在Rust中,最常見的兩種控制流結(jié)構(gòu)是if
表達(dá)式和循環(huán)。
二、if 表達(dá)式
1、語(yǔ)法格式
這里的 condition 必須是 bool 類型
if condition {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
2、多個(gè)
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 conditions are false
}
3、獲取表達(dá)式的值
正如這一小節(jié)的標(biāo)題所說,
if
其實(shí)是一個(gè)表達(dá)式,具有返回值。需要注意的是,
if
分支和else
分支的返回值必須是同一類型。
fn main() {
let temperature = 20;
let weather = if temperature >= 25 {
"hot"
} else {
"cool"
};
println!("The weather today is {}.", weather);
}
三、循環(huán)
Rust中提供了三種循環(huán)方式,
loop
,while
,for
。
1、loop:無限循環(huán),可跳出
無限循環(huán)
loop
關(guān)鍵字會(huì)創(chuàng)建一個(gè)無限循環(huán)
loop {
// code to execute repeatedly
}
跳出循環(huán)
想要從循環(huán)中跳出,需要配合
break
關(guān)鍵詞使用,下面的代碼也展示了 continue 的用法!
let mut counter = 0;
loop {
counter += 1;
if counter < 5 {
continue;
}
println!("Hello, world!");
if counter >= 5 {
break;
}
}
返回值
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:條件循環(huán),可跳出
while condition {
// code to execute while the condition is true
}
3、for:常用于訪問集合
訪問集合:while 示例
fn main() {
let numbers = [1, 2, 3, 4, 5];
let mut index = 0;
while index < numbers.len() {
println!("The value is: {}", numbers[index]);
index += 1;
}
}
訪問集合:for 示例
fn main() {
let numbers = [1, 2, 3, 4, 5];
for number in numbers {
println!("The value is: {}", number);
}
}
對(duì)一個(gè) range 進(jìn)行循環(huán)
這里的
1..=3
表示[1,3]
這個(gè)區(qū)間的整數(shù)。如果是左閉右開,要寫成1..3
。
fn main() {
for x in 1..=3 {
println!("x: {}", x);
}
}
4、labels:給循環(huán)加標(biāo)簽
三種循環(huán)都支持!
當(dāng)循環(huán)存在嵌套關(guān)系時(shí),break
和continue
只會(huì)對(duì)最內(nèi)層的循環(huán)生效。但是有時(shí)候我們希望可以對(duì)外層的循環(huán)做break
或者continue
,這時(shí)該怎么辦?幸運(yùn)的是,Rust 可以給循環(huán)加上標(biāo)簽,從而break
和continue
都可以直接操作標(biāo)簽。文章來源:http://www.zghlxwxcb.cn/news/detail-696363.html
fn main() {
let x = 1;
'outer: loop {
let mut y = 1;
'inner: loop {
if y == 3 {
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
}
}
}
}
帶返回值文章來源地址http://www.zghlxwxcb.cn/news/detail-696363.html
fn main() {
let x = 1;
let z = 'outer: loop {
let mut y = 1;
'inner: loop {
if y == 3 {
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);
}
到了這里,關(guān)于【Rust】003-基礎(chǔ)語(yǔ)法:流程控制的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!