flow-control #174
Replies: 35 comments 4 replies
-
done, 简单的流程控制代码,和其他语言差别不大,在loop方面有点像汇编语言 |
Beta Was this translation helpful? Give feedback.
-
最后一个文档没有提到 |
Beta Was this translation helpful? Give feedback.
-
Done. |
Beta Was this translation helpful? Give feedback.
-
最后一题,continue ‘outer; 2022年11月3号,必须换成break才行了吗? |
Beta Was this translation helpful? Give feedback.
-
2023.2.11,完成。 |
Beta Was this translation helpful? Give feedback.
-
`label这个语法真逆天,像goto。 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
// 1.
// let n = 5;
// if n < 0 {
// println!("{} is negative", n);
// } else if n > 0 {
// println!("{} is positive", n);
// } else {
// println!("{} is zero", n);
// }
// println!("Hello, world!");
// 2.
// let n = 5;
// let big_n =
// if n < 10 && n > -10 {
// println!("small number, increase ten-fold");
// 10 * n
// } else {
// println!("big number, reduce by two");
// n / 2
// };
// println!("{} -> {}", n, big_n);
// 3.
// for n in 1..100 {
// if n == 100 {
// panic!("Never let this run");
// }
// }
// 4.
// let names = [String::from("Baker"), String::from("Charlie"), String::from("Alice"), String::from("Delta")];
// for name in &names {
// }
// println!("{:#?}", names);
// let numbers = [1, 2, 3, 4, 5];
// for number in numbers {
// }
// println!("{:#?}", numbers);
// 5.
// let a = [10, 20, 30, 40, 50];
// for (i, v) in a.iter().enumerate() {
// println!("index is: {}, value is: {}", i, v);
// }
// 6.
// let mut n = 1;
// while n <= 10 {
// if n % 15 == 0 {
// println!("fizzbuzz");
// } else if n % 3 == 0 {
// println!("fizz");
// } else if n % 5 == 0 {
// println!("buzz");
// } else {
// println!("{}", n);
// }
// n += 1;
// }
// println!("n is {}", n);
// 7.
// let mut n = 0;
// for i in 0..=100 {
// if n == 66 {
// // break ; // 最后一个;有什么实际意义呢?
// break
// }
// n += i;
// }
// assert_eq!(n, 66);
// 8.
// let mut n = 0;
// for i in 0..=100 {
// if n != 66 {
// n += 1;
// continue;
// }
// // n += i;
// println!("n is {}", n);
// }
// assert_eq!(n, 66);
// 9.
// let mut count = 0u32;
// println!("Let's count until infinity!");
// loop {
// count += 1;
// if count == 3 {
// println!("three");
// continue;
// }
// println!("{}", count);
// if count == 5 {
// println!("OK, that's enough");
// break;
// }
// }
// assert_eq!(count, 5);
// 10.
// let mut counter = 0;
// let result = loop {
// counter += 1;
// if counter == 10 {
// break counter * 2;
// }
// };
// assert_eq!(result, 20);
// 11.
let mut count = 0;
'outer: loop {
'inner1: loop {
if count >= 20 {
break 'inner1;
}
count += 2;
}
count += 5;
'inner2: loop {
if count >= 30 {
break 'outer;
}
continue 'outer;
}
assert!(count == 30);
}
} |
Beta Was this translation helpful? Give feedback.
-
Done. |
Beta Was this translation helpful? Give feedback.
-
用break 返回值到底是语句还是表达式?加上去掉 |
Beta Was this translation helpful? Give feedback.
-
这样的 loop 比较好,有效避免了跳出多重循环的时候,必须要用 goto 的写法。 |
Beta Was this translation helpful? Give feedback.
-
Done, This tutorial is pretty nice!! |
Beta Was this translation helpful? Give feedback.
-
done,总体难度还是蛮简单的 |
Beta Was this translation helpful? Give feedback.
-
第8题去掉__ 也能正常运行 😂 // 填空,不要修改其它代码
fn main() {
let mut n = 0;
for i in 0..=100 {
if n != 66 {
n+=1;
}
}
assert_eq!(n, 66);
} |
Beta Was this translation helpful? Give feedback.
-
flow-control
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/flow-control.html
Beta Was this translation helpful? Give feedback.
All reactions