diff options
-rw-r--r-- | largest/.gitignore | 18 | ||||
-rw-r--r-- | largest/Cargo.toml | 9 | ||||
-rw-r--r-- | largest/src/main.rs | 23 |
3 files changed, 50 insertions, 0 deletions
diff --git a/largest/.gitignore b/largest/.gitignore new file mode 100644 index 0000000..e629269 --- /dev/null +++ b/largest/.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/largest/Cargo.toml b/largest/Cargo.toml new file mode 100644 index 0000000..aeb611b --- /dev/null +++ b/largest/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "largest" +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/largest/src/main.rs b/largest/src/main.rs new file mode 100644 index 0000000..fbbf4d4 --- /dev/null +++ b/largest/src/main.rs @@ -0,0 +1,23 @@ +fn largest<T: PartialOrd + Copy>(list: &[T]) -> T { + let mut largest = list[0]; + + for &item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let result = largest(&number_list); + println!("The largest number is: {}", result); + + let char_list = vec!['y', 'm', 'a', 'q']; + + let result = largest(&char_list); + println!("The largest char is: {}", result); +} |