mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
2 years ago
|
## own_and_return
|
||
2 years ago
|
|
||
|
### Instructions
|
||
|
|
||
2 years ago
|
Create a struct `Film` that has one field `name` of type `String`.
|
||
2 years ago
|
|
||
2 years ago
|
Create the following two functions:
|
||
|
|
||
2 years ago
|
- Create a function `take_film_name`, it will return the name and consume the film (you should not be able to reuse it after you passed it to the function).
|
||
|
- Create a function `read_film_name`, it will return the name without consuming the film (you can call the function multiple times with the same argument).
|
||
2 years ago
|
|
||
|
### Expected functions
|
||
|
|
||
|
```rust
|
||
2 years ago
|
pub struct Film {
|
||
|
pub name: String,
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
pub fn read_film_name(/* to be implemented */) -> String {
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
pub fn take_film_name(/* to be implemented */) -> String {
|
||
2 years ago
|
}
|
||
|
|
||
|
|
||
2 years ago
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
2 years ago
|
Here is a possible program to test your function:
|
||
2 years ago
|
|
||
|
```rust
|
||
2 years ago
|
use own_and_return::*;
|
||
2 years ago
|
|
||
2 years ago
|
|
||
2 years ago
|
pub struct Film {
|
||
|
pub name: String,
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
fn main() {
|
||
2 years ago
|
let my_film = Film { name: "Terminator".toString() };
|
||
|
println!("{}", take_film_name(/* to be implemented */));
|
||
2 years ago
|
// the order of the print statements is intentional, if your implementation is correct,
|
||
|
// you should have a compile error because my_film was consumed
|
||
2 years ago
|
println!("{}", read_film_name(/* to be implemented */));
|
||
|
println!("{}", take_film_name(/*to be implemented*/))
|
||
2 years ago
|
// you can test this function by commenting out the first print statement,
|
||
|
// you should see the expected output without errors in this case
|
||
2 years ago
|
}
|
||
2 years ago
|
```
|
||
2 years ago
|
|
||
2 years ago
|
And its output:
|
||
|
|
||
|
```console
|
||
|
$ cargo run
|
||
|
Terminator
|
||
|
Terminator
|
||
|
$
|
||
|
```
|