if-else来做分支控制
if else 无处不在 — 鲁迅
if condition == true {
// A...
} else {
// B...
}
和平常的没什么区别
fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!("The value of number is: {}", number);
}
Notice:
if语句块是表达式,使用if表达式的返回值来给number进行赋值:number的值是5- 用
if来赋值时,要保证每个分支返回的类型一样(不准确),否则就会Error!!!
使用 else if 来处理多重条件
fn main() {
let n = 6;
if n % 4 == 0 {
println!("number is divisible by 4");
} else if n % 3 == 0 {
println!("number is divisible by 3");
} else if n % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
循环控制
for循环
for循环是rust的大杀器
fn main(){
for i in 1..=5{
println!("{}",i);
}
}
循环输出1个1-5的序列,简单粗暴
我们在使用for时往往使用集合的引用形式,除非不想再后面的代码中继续使用该集合,(比如我们使用了container的引用);如果不适用引用的化,所有权会被转移到for语句块中,后面就无法使用这个集合了:
for item in &container{
//
}
对于实现了
copy特征的数组,for item in arr并不会把arr的所有权转移,而是直接对其进行了copy,因此循环之后仍然可以使用arr
如果想要在循环中,修改该元素,使用mut关键字:
for item in &mut collection{
//
}
一些总结:
| 使用方法 | 等价 | 所有权 |
|---|---|---|
for item in collection | for item in IntoIterator::into_iter(collection) | 转移所有权 |
for item in &collection | for item in collection.iter() | 不可变借用 |
for item in &mut collection | for item in collection.iter_mut() | 可变借用 |
| 如果想要在循环中获取元素的索引: |
fn main(){
let a = [4,3,2,1];
// `.iter()`将`a`转换为一个迭代器
for (i,v) in a.iter().eumerate(){
println!("第{}个元素是{}",i+1,v);
}
}
当我们不想声明变量时:
for _ in 0..10{
//..
}
2中循环方式的优劣对比:
let collection = [1,2,3,4,5];
for i in 0.collection.len(){
let item = collection[i];
//
}
for item in collection{
}
对比:
- 性能:第一种使用方式中
collection[index]中的索引访问,会因为边界检查(Bounds Checking)导致运行时的性能损耗-Rust会检查index是否落在集合内,到那时第二种直接迭代就不会除法这种检查,因为编译器在编译的时候就会证明这种访问时合法的 - 安全:第一种方式里对
collections的索引访问时非连续的,存在一定可能性在2次访问之间,collections发生了变化,导致了脏数据的产生;第二种直接迭代的方式是连续访问,因此不存在这种风险
continue
跳过本次循环,直接下一次循环
break
使用break直接跳出整个循环
while循环
嗯
fn main() {
let mut n = 0;
while n <= 5 {
println!("{}!", n);
n = n + 1;
}
println!("111!");
}
也可以通过loop+if+break
fn main(){
let mut n = 0;
loop{
if n>5{
break
}
n = n+1;
}
println!("111");
}
while Vs for
也可以使用while来实现for的功能:
fn main(){
let a = [10,20,30,40,50];
let mut index = 0;
while index<5{
println!("The value is:{}",a[index]);
index = index+1;
}
}
看似很平常,但是这个过程很容易出错,如果索引长度不正确就会panic,这也使得程序更慢,因为编译器加了运行时代码会对每次循环的每个元素进行条件检查
for main(){
let a = [10,20,30,40,50];
for element in a.iter(){
println!("the value is:{}",element);
}
}
loop循环
对于循环而言,loop循环时适用面最高的,可以适用于所有循环场景,但是很哆嗦
嗯,这种就坏菜了
fn main(){
loop{
println!("again!");
}
}
使用loop时,必不可少的是break关键字
fn main(){
let mut counter = 0;
let result = loop{
counter += 1;
if counter == 10{
break counter*2;
}
};
println!("The result is {}",result);
}
Notice:
- break可以单独使用,也可以带一个返回值,类似于
return - loop是一个表达式,因此可以返回一个值