From d24959d0056ee05de94bfadfd8db2747f2e8c6e8 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 6 Dec 2020 11:44:17 +0100 Subject: [PATCH] lifetimes, tests and traits --- lifetimes/Cargo.toml | 9 +++ lifetimes/src/main.rs | 15 +++++ minigrep/Cargo.toml | 9 +++ minigrep/src/main.rs | 6 ++ tests/Cargo.toml | 9 +++ tests/src/lib.rs | 116 ++++++++++++++++++++++++++++++++ tests/tests/integration_test.rs | 6 ++ traits/Cargo.toml | 9 +++ traits/src/main.rs | 23 +++++++ 9 files changed, 202 insertions(+) create mode 100644 lifetimes/Cargo.toml create mode 100644 lifetimes/src/main.rs create mode 100644 minigrep/Cargo.toml create mode 100644 minigrep/src/main.rs create mode 100644 tests/Cargo.toml create mode 100644 tests/src/lib.rs create mode 100644 tests/tests/integration_test.rs create mode 100644 traits/Cargo.toml create mode 100644 traits/src/main.rs diff --git a/lifetimes/Cargo.toml b/lifetimes/Cargo.toml new file mode 100644 index 0000000..fd87a9e --- /dev/null +++ b/lifetimes/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "lifetimes" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/lifetimes/src/main.rs b/lifetimes/src/main.rs new file mode 100644 index 0000000..90529e8 --- /dev/null +++ b/lifetimes/src/main.rs @@ -0,0 +1,15 @@ +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { + x + } else { + y + } +} + +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} \ No newline at end of file diff --git a/minigrep/Cargo.toml b/minigrep/Cargo.toml new file mode 100644 index 0000000..396baa6 --- /dev/null +++ b/minigrep/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "minigrep" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs new file mode 100644 index 0000000..8733eaf --- /dev/null +++ b/minigrep/src/main.rs @@ -0,0 +1,6 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + println!("{:?}", args); +} \ No newline at end of file diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 0000000..78fcd89 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "tests" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/src/lib.rs b/tests/src/lib.rs new file mode 100644 index 0000000..527fd84 --- /dev/null +++ b/tests/src/lib.rs @@ -0,0 +1,116 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() -> Result<(), String> { + if 2 + 2 == 4 { + Ok(()) + } else { + Err(String::from("two plus two does not equal four")) + } + } + + #[test] + fn exploration() { + assert_eq!(2 + 2, 4); + } + + use super::*; + + #[test] + fn larger_can_hold_smaller() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(larger.can_hold(&smaller)); + } + + #[test] + fn smaller_cannot_hold_larger() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(!smaller.can_hold(&larger)); + } + + #[test] + fn it_adds_two() { + assert_eq!(4, add_two(2)); + } + + #[test] + fn greeting_contains_name() { + let result = greeting("Carol"); + assert!( + result.contains("Carol"), + "Greeting did not contain name, value was `{}`", + result + ); + } + + #[test] + #[should_panic] + fn greater_than_100_or_less_than_1() { + Guess::new(200); + } + + #[test] + #[should_panic(expected = "Guess value must be less than or equal to 100")] + fn greater_than_100() { + Guess::new(200); + } +} + +pub fn greeting(name: &str) -> String { + format!("Hello {}!", name) +} + +pub fn add_two(a: i32) -> i32 { + a + 2 +} + +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} + +pub struct Guess { + value: i32, +} + +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 { + panic!( + "Guess value must be greater than or equal to 1, got {}.", + value + ); + } else if value > 100 { + panic!( + "Guess value must be less than or equal to 100, got {}.", + value + ); + } + + Guess { value } + } +} + diff --git a/tests/tests/integration_test.rs b/tests/tests/integration_test.rs new file mode 100644 index 0000000..59a1456 --- /dev/null +++ b/tests/tests/integration_test.rs @@ -0,0 +1,6 @@ +use tests; + +#[test] +fn it_adds_two() { + assert_eq!(4, tests::add_two(2)); +} \ No newline at end of file diff --git a/traits/Cargo.toml b/traits/Cargo.toml new file mode 100644 index 0000000..594ab0a --- /dev/null +++ b/traits/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "traits" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/traits/src/main.rs b/traits/src/main.rs new file mode 100644 index 0000000..3d17e49 --- /dev/null +++ b/traits/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); +} \ No newline at end of file