diff --git a/rustbyexample/flow_control.d/for.rs b/rustbyexample/flow_control.d/for.rs new file mode 100644 index 0000000..0698854 --- /dev/null +++ b/rustbyexample/flow_control.d/for.rs @@ -0,0 +1,13 @@ +fn main() { + 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); + } + } +} diff --git a/rustbyexample/flow_control.d/for2.rs b/rustbyexample/flow_control.d/for2.rs new file mode 100644 index 0000000..e103bfc --- /dev/null +++ b/rustbyexample/flow_control.d/for2.rs @@ -0,0 +1,13 @@ +fn main() { + 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); + } + } +} diff --git a/rustbyexample/flow_control.d/for3.rs b/rustbyexample/flow_control.d/for3.rs new file mode 100644 index 0000000..731bb5d --- /dev/null +++ b/rustbyexample/flow_control.d/for3.rs @@ -0,0 +1,12 @@ +fn main() { + let names = vec!["Bob", "Frank", "Ferris"]; + + for name in names.iter() { + match name { + &"Ferris" => println!("There is a rustacean among us!"), + _ => println!("Hello {}", name), + } + } + + println!("names: {:?}", names); +} diff --git a/rustbyexample/flow_control.d/for4.rs b/rustbyexample/flow_control.d/for4.rs new file mode 100644 index 0000000..ddeb575 --- /dev/null +++ b/rustbyexample/flow_control.d/for4.rs @@ -0,0 +1,12 @@ +fn main() { + 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); +} diff --git a/rustbyexample/flow_control.d/for5.rs b/rustbyexample/flow_control.d/for5.rs new file mode 100644 index 0000000..7974561 --- /dev/null +++ b/rustbyexample/flow_control.d/for5.rs @@ -0,0 +1,12 @@ +fn main() { + 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); +}