#[test]
fn test_looping() {
let index: usize = 2;
while let Some(index) = index.checked_sub(1) {
println!("{}", index);
}
}
为什么这会造成无限循环?我预计while let
表达式会覆盖index
.取而代之的是,我必须添加几行额外的行才能将其设置为Mutations index
.
#[test]
fn test_looping() {
let index: usize = 2;
while let Some(index) = index.checked_sub(1) {
println!("{}", index);
}
}
为什么这会造成无限循环?我预计while let
表达式会覆盖index
.取而代之的是,我必须添加几行额外的行才能将其设置为Mutations index
.
let
声明一个新的绑定,跟踪前一个绑定.它不会修改现有变量.index
最初没有被宣布为let mut index
是一个迹象,表明它没有发生Mutations .
要修复它,请写下:
#[test]
fn test_looping() {
let mut index: usize = 2;
while let Some(i) = index.checked_sub(1) {
index = i;
println!("{}", index);
}
}