From 638f6d997e76f45a665e2de741e6c4b2b5616aef Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 3 Sep 2020 14:34:26 +0800 Subject: Find the largest T --- largest/.gitignore | 18 ++++++++++++++++++ largest/Cargo.toml | 9 +++++++++ largest/src/main.rs | 23 +++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 largest/.gitignore create mode 100644 largest/Cargo.toml create mode 100644 largest/src/main.rs (limited to 'largest') 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 "] +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(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); +} -- cgit v1.2.3