Merge pull request #163 from lamalex/recursion-in-rust
Add rust examples to 03_recursion
This commit is contained in:
10
03_recursion/rust/01_countdown/.gitignore
vendored
Normal file
10
03_recursion/rust/01_countdown/.gitignore
vendored
Normal file
@@ -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
|
||||||
10
03_recursion/rust/01_countdown/Cargo.toml
Normal file
10
03_recursion/rust/01_countdown/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "countdown"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Alexander Launi <alex.launi@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-traits = "0.2"
|
||||||
17
03_recursion/rust/01_countdown/src/main.rs
Normal file
17
03_recursion/rust/01_countdown/src/main.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use std::ops::Sub;
|
||||||
|
use std::fmt::Display;
|
||||||
|
use num_traits::identities::One;
|
||||||
|
use num_traits::identities::Zero;
|
||||||
|
|
||||||
|
fn countdown<T: Display + One + Zero + Sub<Output = T>>(i: T) {
|
||||||
|
println!("{}", i);
|
||||||
|
if i.is_zero() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
countdown(i - T::one());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
countdown(5);
|
||||||
|
}
|
||||||
10
03_recursion/rust/02_greet/.gitignore
vendored
Normal file
10
03_recursion/rust/02_greet/.gitignore
vendored
Normal file
@@ -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
|
||||||
9
03_recursion/rust/02_greet/Cargo.toml
Normal file
9
03_recursion/rust/02_greet/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "greet"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Alexander Launi <alex.launi@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
18
03_recursion/rust/02_greet/src/main.rs
Normal file
18
03_recursion/rust/02_greet/src/main.rs
Normal file
@@ -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");
|
||||||
|
}
|
||||||
10
03_recursion/rust/03_factorial/.gitignore
vendored
Normal file
10
03_recursion/rust/03_factorial/.gitignore
vendored
Normal file
@@ -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
|
||||||
10
03_recursion/rust/03_factorial/Cargo.toml
Normal file
10
03_recursion/rust/03_factorial/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "factorial"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Alexander Launi <alex.launi@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-traits = "0.2"
|
||||||
17
03_recursion/rust/03_factorial/src/main.rs
Normal file
17
03_recursion/rust/03_factorial/src/main.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use std::ops::Sub;
|
||||||
|
use std::fmt::Display;
|
||||||
|
use num_traits::identities::One;
|
||||||
|
|
||||||
|
fn fact<T: PartialOrd + PartialEq + One + Sub<Output = T> + 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));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user