Browse Source

Add test and subject for exercise `lifetimes`

content-update
Augusto 3 years ago
parent
commit
9cd2c1a994
  1. 12
      rust/tests/lifetimes_test/Cargo.lock
  2. 10
      rust/tests/lifetimes_test/Cargo.toml
  3. 34
      rust/tests/lifetimes_test/src/main.rs
  4. 40
      subjects/lifetimes/README.md

12
rust/tests/lifetimes_test/Cargo.lock diff.generated

@ -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",
]

10
rust/tests/lifetimes_test/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "lifetimes_test"
version = "0.1.0"
authors = ["Augusto <aug.ornelas@gmail.com>"]
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"}

34
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");
}
}

40
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$
```
Loading…
Cancel
Save