mirror of https://github.com/01-edu/public.git
Augusto
4 years ago
4 changed files with 89 additions and 0 deletions
@ -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", |
||||||
|
] |
@ -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"} |
@ -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); |
||||||
|
} |
@ -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…
Reference in new issue