面试题答案
一键面试优化思路
可以提前计算出num
范围检查中涉及到的固定值的相关条件,减少每次循环中的计算量。对于简单的num > 0
条件,在当前场景下优化空间有限,但如果条件更为复杂,比如涉及到多个变量和复杂表达式,可以将固定部分提前计算。
优化后代码
let mut num = 10;
let threshold = 0;
while num > threshold {
println!("The number is: {}", num);
num -= 1;
}