rust-by-practice/solutions/flow-control.md

224 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2022-03-02 07:48:25 -06:00
1.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let n = 5;
if n < 0 {
println!("{} is negative", n);
2022-05-01 00:08:50 -05:00
} else if n > 0 {
2022-03-02 07:48:25 -06:00
println!("{} is positive", n);
} else {
println!("{} is zero", n);
}
}
```
2.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let n = 5;
let big_n =
if n < 10 && n > -10 {
println!(", and is a small number, increase ten-fold");
10 * n
} else {
println!(", and is a big number, halve the number");
2022-05-01 00:08:50 -05:00
n / 2
2022-03-02 07:48:25 -06:00
};
println!("{} -> {}", n, big_n);
}
```
3.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
2022-05-01 00:08:50 -05:00
for n in 1..100 {
2022-03-02 07:48:25 -06:00
if n == 100 {
panic!("NEVER LET THIS RUN")
}
}
}
```
4.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
2022-05-01 00:08:50 -05:00
let names = [String::from("liming"), String::from("hanmeimei")];
2022-03-02 07:48:25 -06:00
for name in &names {
// do something with name...
}
println!("{:?}", names);
let numbers = [1, 2, 3];
// the elements in numbers are Copyļ¼Œso there is no move here
for n in numbers {
// do something with name...
}
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
println!("{:?}", numbers);
}
```
5.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let a = [4, 3, 2, 1];
// iterate the indexing and value in 'a'
2022-05-01 00:08:50 -05:00
for (i, v) in a.iter().enumerate() {
println!("The {}th element is {}", i + 1, v);
2022-03-02 07:48:25 -06:00
}
}
```
6.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
// A counter variable
let mut n = 1;
// Loop while the condition is true
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;
}
2022-05-01 00:08:50 -05:00
println!("n reached {}, soloop is over", n);
2022-03-02 07:48:25 -06:00
}
```
7.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let mut n = 0;
for i in 0..=100 {
2022-05-01 00:08:50 -05:00
if n == 66 {
break;
}
n += 1;
2022-03-02 07:48:25 -06:00
}
assert_eq!(n, 66);
}
```
8.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let mut n = 0;
for i in 0..=100 {
2022-05-01 00:08:50 -05:00
if n != 66 {
n += 1;
continue;
}
break;
2022-03-02 07:48:25 -06:00
}
assert_eq!(n, 66);
}
```
9.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let mut count = 0u32;
println!("Let's count until infinity!");
// Infinite loop
loop {
count += 1;
if count == 3 {
println!("three");
// Skip the rest of this iteration
continue;
}
println!("{}", count);
if count == 5 {
println!("OK, that's enough");
break;
}
}
assert_eq!(count, 5);
}
```
10.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
assert_eq!(result, 20);
}
```
11.
2022-05-01 00:08:50 -05:00
2022-03-02 07:48:25 -06:00
```rust
fn main() {
let mut count = 0;
'outer: loop {
'inner1: loop {
if count >= 20 {
// This would break only the inner1 loop
break 'inner1; // `break` is also ok
}
count += 2;
}
count += 5;
'inner2: loop {
if count >= 30 {
// This breaks the outer loop
break 'outer;
}
// This will continue the outer loop
continue 'outer;
}
}
assert!(count == 30)
}
```