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$ +```