mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 01:52:42 +00:00
83 lines
1.7 KiB
Rust
83 lines
1.7 KiB
Rust
// ./src/flow_control/for.md
|
|
|
|
|
|
fn part0() {
|
|
// `n` will take the values: 1, 2, ..., 100 in each iteration
|
|
for n in 1..101 {
|
|
if n % 15 == 0 {
|
|
println!("fizzbuzz");
|
|
} else if n % 3 == 0 {
|
|
println!("fizz");
|
|
} else if n % 5 == 0 {
|
|
println!("buzz");
|
|
} else {
|
|
println!("{}", n);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn part1() {
|
|
// `n` will take the values: 1, 2, ..., 100 in each iteration
|
|
for n in 1..=100 {
|
|
if n % 15 == 0 {
|
|
println!("fizzbuzz");
|
|
} else if n % 3 == 0 {
|
|
println!("fizz");
|
|
} else if n % 5 == 0 {
|
|
println!("buzz");
|
|
} else {
|
|
println!("{}", n);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn part2() {
|
|
let names = vec!["Bob", "Frank", "Ferris"];
|
|
|
|
for name in names.iter() {
|
|
match name {
|
|
&"Ferris" => println!("There is a rustacean among us!"),
|
|
// TODO ^ Try deleting the & and matching just "Ferris"
|
|
_ => println!("Hello {}", name),
|
|
}
|
|
}
|
|
|
|
println!("names: {:?}", names);
|
|
}
|
|
|
|
fn part3() {
|
|
let names = vec!["Bob", "Frank", "Ferris"];
|
|
|
|
for name in names.into_iter() {
|
|
match name {
|
|
"Ferris" => println!("There is a rustacean among us!"),
|
|
_ => println!("Hello {}", name),
|
|
}
|
|
}
|
|
|
|
// println!("names: {:?}", names);
|
|
// FIXME ^ Comment out this line
|
|
}
|
|
|
|
fn part4() {
|
|
let mut names = vec!["Bob", "Frank", "Ferris"];
|
|
|
|
for name in names.iter_mut() {
|
|
*name = match name {
|
|
&mut "Ferris" => "There is a rustacean among us!",
|
|
_ => "Hello",
|
|
}
|
|
}
|
|
|
|
println!("names: {:?}", names);
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
part1();
|
|
part2();
|
|
part3();
|
|
part4();
|
|
}
|
|
|