diff options
Diffstat (limited to 'control-flow')
-rw-r--r-- | control-flow/.gitignore | 18 | ||||
-rw-r--r-- | control-flow/Cargo.toml | 9 | ||||
-rw-r--r-- | control-flow/src/main.rs | 63 |
3 files changed, 90 insertions, 0 deletions
diff --git a/control-flow/.gitignore b/control-flow/.gitignore new file mode 100644 index 0000000..e629269 --- /dev/null +++ b/control-flow/.gitignore @@ -0,0 +1,18 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# End of https://www.toptal.com/developers/gitignore/api/rust + diff --git a/control-flow/Cargo.toml b/control-flow/Cargo.toml new file mode 100644 index 0000000..b17539f --- /dev/null +++ b/control-flow/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "control-flow" +version = "0.1.0" +authors = ["Jordan Gong <jordan.gong@protonmail.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/control-flow/src/main.rs b/control-flow/src/main.rs new file mode 100644 index 0000000..d3200f0 --- /dev/null +++ b/control-flow/src/main.rs @@ -0,0 +1,63 @@ +fn main() { + /* if */ + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } + + let number = 6; + + if number % 4 == 0 { + println!("number is divisible by 4"); + } else if number % 3 == 0 { + println!("number is divisible by 3"); + } else if number % 2 == 0 { + println!("number is divisible by 2"); + } else { + println!("number is not divisible by 4, 3 or 2"); + } + + let condition = true; + let number = if condition { 5 } else { 6 }; + + println!("The value of number is: {}", number); + + /* loop */ + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is: {}", result); + + /* while */ + let mut number = 3; + + while number != 0 { + println!("{}!", number); + + number -= 1; + } + + println!("LIFTOFF!!!"); + + /* for */ + let a = [10, 20, 30, 40, 50]; + + for element in a.iter() { + println!("the value is: {}", element); + } + + for number in (1..4).rev() { + println!("{}!", number); + } + println!("LIFTOFF!!!"); +} |