Browse Source

Add subject and test for the exercise `generic`

content-update
Augusto 3 years ago
parent
commit
4064c5480c
  1. 12
      rust/tests/generics_test/Cargo.lock
  2. 10
      rust/tests/generics_test/Cargo.toml
  3. 35
      rust/tests/generics_test/src/main.rs
  4. 32
      subjects/generics/README.md

12
rust/tests/generics_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 = "generics"
version = "0.1.0"
[[package]]
name = "generics_test"
version = "0.1.0"
dependencies = [
"generics",
]

10
rust/tests/generics_test/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "generics_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]
generics = { path = "../../../../rust-piscine-solutions/generics"}

35
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);
}

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