2020-12-27 19:04:52 +00:00
|
|
|
## generics
|
|
|
|
|
|
|
|
### Instructions
|
|
|
|
|
2022-06-08 03:46:34 +00:00
|
|
|
Create a **function** named `identity` which calculates the identity of a value (receives any data type and returns the same value).
|
2020-12-27 19:04:52 +00:00
|
|
|
|
2022-03-23 18:33:24 +00:00
|
|
|
|
|
|
|
|
2021-03-08 04:07:11 +00:00
|
|
|
### Expected Function (signature to be completed)
|
2020-12-27 19:04:52 +00:00
|
|
|
|
|
|
|
```rust
|
2021-03-08 04:07:11 +00:00
|
|
|
pub fn identity(v: _) -> _ {
|
2020-12-27 19:04:52 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
|
|
Here is a program to test your function.
|
|
|
|
|
|
|
|
```rust
|
2021-03-08 04:07:11 +00:00
|
|
|
use generics::*;
|
|
|
|
|
2020-12-27 19:04:52 +00:00
|
|
|
fn main() {
|
|
|
|
println!("{}", identity("Hello, world!"));
|
|
|
|
println!("{}", identity(3));
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-03-08 04:07:11 +00:00
|
|
|
And its output:
|
2020-12-27 19:04:52 +00:00
|
|
|
|
|
|
|
```console
|
2021-04-28 09:47:34 +00:00
|
|
|
$ cargo run
|
2020-12-27 19:04:52 +00:00
|
|
|
Hello, world!
|
|
|
|
3
|
2021-04-28 09:47:34 +00:00
|
|
|
$
|
2020-12-27 19:04:52 +00:00
|
|
|
```
|
2022-06-08 03:46:34 +00:00
|
|
|
|
|
|
|
### Notions
|
|
|
|
|
|
|
|
- [Generics](https://doc.rust-lang.org/book/ch10-01-syntax.html)
|