From 4064c5480c87beff6bbea0ed4f4d8f998e483f96 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 19:04:52 +0000 Subject: [PATCH] Add subject and test for the exercise `generic` --- rust/tests/generics_test/Cargo.lock | 12 ++++++++++ rust/tests/generics_test/Cargo.toml | 10 ++++++++ rust/tests/generics_test/src/main.rs | 35 ++++++++++++++++++++++++++++ subjects/generics/README.md | 32 +++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 rust/tests/generics_test/Cargo.lock create mode 100644 rust/tests/generics_test/Cargo.toml create mode 100644 rust/tests/generics_test/src/main.rs create mode 100644 subjects/generics/README.md diff --git a/rust/tests/generics_test/Cargo.lock b/rust/tests/generics_test/Cargo.lock new file mode 100644 index 00000000..c5418bd2 --- /dev/null +++ b/rust/tests/generics_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "generics" +version = "0.1.0" + +[[package]] +name = "generics_test" +version = "0.1.0" +dependencies = [ + "generics", +] diff --git a/rust/tests/generics_test/Cargo.toml b/rust/tests/generics_test/Cargo.toml new file mode 100644 index 00000000..7f767294 --- /dev/null +++ b/rust/tests/generics_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "generics_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] +generics = { path = "../../../../rust-piscine-solutions/generics"} \ No newline at end of file diff --git a/rust/tests/generics_test/src/main.rs b/rust/tests/generics_test/src/main.rs new file mode 100644 index 00000000..f95b0023 --- /dev/null +++ b/rust/tests/generics_test/src/main.rs @@ -0,0 +1,35 @@ +// Write a functions called identity that calculates the identity of a +// value (receives any data type and returns the same value) +use generics::*; + +fn main() { + println!("{}", identity("Hello, world!")); + println!("{}", identity(3)); +} + +#[derive(PartialEq, Debug)] +struct Point { + x: i32, + y: i32, +} + +#[test] +fn test_with_int() { + assert_eq!(identity(3), 3); +} + +#[test] +fn test_with_float() { + assert_eq!(identity(1.0), 1.0); +} + +#[test] +fn test_with_str() { + assert_eq!(identity("you"), "you"); +} + +#[test] +fn test_with_struct() { + let s = Point { x: 1, y: 2 }; + assert_eq!(identity(&s), &s); +} diff --git a/subjects/generics/README.md b/subjects/generics/README.md new file mode 100644 index 00000000..a93fba66 --- /dev/null +++ b/subjects/generics/README.md @@ -0,0 +1,32 @@ +## generics + +### Instructions + +Write a functions called identity that calculates the identity of a value (receives any data type and returns the same value) + +### Expected Function + +```rust +fn identity(v: _) -> _ { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + println!("{}", identity("Hello, world!")); + println!("{}", identity(3)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Hello, world! +3 +student@ubuntu:~/[[ROOT]]/test$ +```