From 9cd2c1a9947db5df641b882acea2cf7b681ad0d6 Mon Sep 17 00:00:00 2001 From: Augusto Date: Mon, 28 Dec 2020 19:03:57 +0000 Subject: [PATCH] Add test and subject for exercise `lifetimes` --- rust/tests/lifetimes_test/Cargo.lock | 12 ++++++++ rust/tests/lifetimes_test/Cargo.toml | 10 +++++++ rust/tests/lifetimes_test/src/main.rs | 34 +++++++++++++++++++++++ subjects/lifetimes/README.md | 40 +++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 rust/tests/lifetimes_test/Cargo.lock create mode 100644 rust/tests/lifetimes_test/Cargo.toml create mode 100644 rust/tests/lifetimes_test/src/main.rs create mode 100644 subjects/lifetimes/README.md diff --git a/rust/tests/lifetimes_test/Cargo.lock b/rust/tests/lifetimes_test/Cargo.lock new file mode 100644 index 00000000..a425a190 --- /dev/null +++ b/rust/tests/lifetimes_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "lifetimes" +version = "0.1.0" + +[[package]] +name = "lifetimes_test" +version = "0.1.0" +dependencies = [ + "lifetimes", +] diff --git a/rust/tests/lifetimes_test/Cargo.toml b/rust/tests/lifetimes_test/Cargo.toml new file mode 100644 index 00000000..cfe2716e --- /dev/null +++ b/rust/tests/lifetimes_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "lifetimes_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lifetimes = { path = "../../../../rust-piscine-solutions/lifetimes"} diff --git a/rust/tests/lifetimes_test/src/main.rs b/rust/tests/lifetimes_test/src/main.rs new file mode 100644 index 00000000..c970a98f --- /dev/null +++ b/rust/tests/lifetimes_test/src/main.rs @@ -0,0 +1,34 @@ +// Create a struct called Person that has two fields: name of type +// string slice (&str) and age of type u8 +// and create the associated function new which creates a new person +// with age 0 and with the name given + +use lifetimes::Person; + +fn main() { + let person = Person::new("Leo"); + + println!("Person = {:?}", person); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fields() { + let person = Person { + name: "Dijkstra", + age: 10, + }; + assert_eq!(person.age, 10); + assert_eq!(person.name, "Dijkstra"); + } + + #[test] + fn create_person() { + let person = Person::new("Leo"); + assert_eq!(person.age, 0); + assert_eq!(person.name, "Leo"); + } +} diff --git a/subjects/lifetimes/README.md b/subjects/lifetimes/README.md new file mode 100644 index 00000000..3756d4fc --- /dev/null +++ b/subjects/lifetimes/README.md @@ -0,0 +1,40 @@ +## lifetimes + +### Instructions + +Create a struct called Person that has two fields: name of type string slice (&str) and age of type u8 and create the associated function new which creates a new person with age 0 and with the name given + +### Expected Functions and Data Structures + +```rust +#[derive(Debug)] +pub struct Person{ + pub name: &str, + pub age: u8, +} + +impl Person { + pub fn new(name: &str) -> Person { + } +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let person = Person::new("Leo"); + + println!("Person = {:?}", person); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Person = Person { name: "Leo", age: 0 } +student@ubuntu:~/[[ROOT]]/test$ +```