public/subjects/copy/README.md

62 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2020-12-25 21:32:57 +00:00
## copy
### Instructions
2022-05-23 09:04:10 +00:00
Create the following **functions**. The objective is to know how ownership works with different types.
2020-12-25 21:32:57 +00:00
2021-02-07 21:49:17 +00:00
- `nbr_function` returns a tuple:
2022-05-23 09:04:10 +00:00
- with the `original` value.
- the `exponential function` of the value.
- and the `natural logarithm` of the `absolute` value.
2021-02-07 21:49:17 +00:00
- `str_function` returns a tuple:
2022-05-23 09:04:10 +00:00
- with the `original` value.
- and the `exponential function` of each value as a string (see the example).
2021-02-07 21:49:17 +00:00
- `vec_function` returns a tuple:
2022-05-23 09:04:10 +00:00
- with the `original` value.
- and the `natural logarithm` of each `absolute` value.
2020-12-25 21:32:57 +00:00
2022-05-23 17:32:53 +00:00
### Expected functions
2020-12-25 21:32:57 +00:00
```rust
pub fn nbr_function(c: i32) -> (i32, f64, f64) {
2020-12-25 21:32:57 +00:00
}
pub fn str_function(a: String) -> (String, String) {
}
pub fn vec_function(b: Vec<i32>) -> (Vec<i32>, Vec<f64>) {
2020-12-25 21:32:57 +00:00
}
```
### Usage
Here is a possible program to test your function :
```rust
2021-02-07 21:49:17 +00:00
use copy::*;
2020-12-25 21:32:57 +00:00
fn main() {
let a: i32 = 0;
2021-02-07 21:49:17 +00:00
let b = String::from("1 2 4 5 6");
let c = vec![1, 2, 4, 5];
println!("{:?}", nbr_function(a));
println!("{:?}", str_function(b));
println!("{:?}", vec_function(c));
2020-12-25 21:32:57 +00:00
}
```
And its output:
```console
$ cargo run
(0, 1.0, -inf)
2020-12-25 21:32:57 +00:00
("1 2 4 5 6", "2.718281828459045 7.38905609893065 54.598150033144236 148.4131591025766 403.4287934927351")
2021-02-07 21:49:17 +00:00
([1, 2, 4, 5], [0.0, 0.6931471805599453, 1.3862943611198906, 1.6094379124341003])
$
2020-12-25 21:32:57 +00:00
```
2022-05-23 09:04:10 +00:00
### Notions
- [scope](https://doc.rust-lang.org/rust-by-example/scope/move.html)
- [Primitive Type f64](https://doc.rust-lang.org/std/primitive.f64.html)