diff --git a/03_recursion/rust/01_countdown/.gitignore b/03_recursion/rust/01_countdown/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/03_recursion/rust/01_countdown/.gitignore @@ -0,0 +1,10 @@ +# 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 diff --git a/03_recursion/rust/01_countdown/Cargo.toml b/03_recursion/rust/01_countdown/Cargo.toml new file mode 100644 index 0000000..9d582e3 --- /dev/null +++ b/03_recursion/rust/01_countdown/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "countdown" +version = "0.1.0" +authors = ["Alexander Launi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num-traits = "0.2" \ No newline at end of file diff --git a/03_recursion/rust/01_countdown/src/main.rs b/03_recursion/rust/01_countdown/src/main.rs new file mode 100644 index 0000000..84ae0e7 --- /dev/null +++ b/03_recursion/rust/01_countdown/src/main.rs @@ -0,0 +1,17 @@ +use std::ops::Sub; +use std::fmt::Display; +use num_traits::identities::One; +use num_traits::identities::Zero; + +fn countdown>(i: T) { + println!("{}", i); + if i.is_zero() { + return + } + + countdown(i - T::one()); +} + +fn main() { + countdown(5); +} diff --git a/03_recursion/rust/02_greet/.gitignore b/03_recursion/rust/02_greet/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/03_recursion/rust/02_greet/.gitignore @@ -0,0 +1,10 @@ +# 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 diff --git a/03_recursion/rust/02_greet/Cargo.toml b/03_recursion/rust/02_greet/Cargo.toml new file mode 100644 index 0000000..98268b0 --- /dev/null +++ b/03_recursion/rust/02_greet/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "greet" +version = "0.1.0" +authors = ["Alexander Launi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/03_recursion/rust/02_greet/src/main.rs b/03_recursion/rust/02_greet/src/main.rs new file mode 100644 index 0000000..8f90eb0 --- /dev/null +++ b/03_recursion/rust/02_greet/src/main.rs @@ -0,0 +1,18 @@ +fn greet2(name: &str) { + println!("how are you {}?", name); +} + +fn bye() { + println!("ok bye!"); +} + +fn greet(name: &str) { + println!("hello {}!", name); + greet2(name); + println!("getting ready to say bye..."); + bye(); +} + +fn main() { + greet("adit"); +} diff --git a/03_recursion/rust/03_factorial/.gitignore b/03_recursion/rust/03_factorial/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/03_recursion/rust/03_factorial/.gitignore @@ -0,0 +1,10 @@ +# 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 diff --git a/03_recursion/rust/03_factorial/Cargo.toml b/03_recursion/rust/03_factorial/Cargo.toml new file mode 100644 index 0000000..223a6ec --- /dev/null +++ b/03_recursion/rust/03_factorial/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "factorial" +version = "0.1.0" +authors = ["Alexander Launi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num-traits = "0.2" diff --git a/03_recursion/rust/03_factorial/src/main.rs b/03_recursion/rust/03_factorial/src/main.rs new file mode 100644 index 0000000..3c77465 --- /dev/null +++ b/03_recursion/rust/03_factorial/src/main.rs @@ -0,0 +1,17 @@ +use std::ops::Sub; +use std::fmt::Display; +use num_traits::identities::One; + +fn fact + Copy + Display>(x: T) -> T { + if x < T::one() { + panic!("Invalid number: {}", x); + } else if x.is_one() { + return T::one(); + } + + return x * fact(x - T::one()); +} + +fn main() { + println!("{}", fact(3)); +} \ No newline at end of file