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.7 KiB
60 lines
1.7 KiB
2 years ago
|
## tuples_refs
|
||
4 years ago
|
|
||
|
### Instructions
|
||
|
|
||
2 years ago
|
- Define a tuple `struct` to represent a `Student`. Each is identified by an id of type `i32`, their first name and last name.
|
||
4 years ago
|
|
||
4 years ago
|
- Then define three **functions** to return the id, first name and last name.
|
||
4 years ago
|
|
||
|
```rust
|
||
|
pub fn id(student: &Student) -> i32 {
|
||
|
}
|
||
|
|
||
|
pub fn first_name(student: &Student) -> String {
|
||
|
}
|
||
|
|
||
|
pub fn last_name(student: &Student) -> String {
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
4 years ago
|
Here is a program to test your functions
|
||
4 years ago
|
|
||
|
```rust
|
||
2 years ago
|
use tuples_refs::*;
|
||
4 years ago
|
|
||
|
fn main() {
|
||
|
let student = Student(20, "Pedro".to_string(), "Domingos".to_string());
|
||
|
println!("Student: {:?}", student);
|
||
|
println!("Student first name: {}", first_name(&student));
|
||
|
println!("Student last name: {}", last_name(&student));
|
||
|
println!("Student Id: {}", id(&student));
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output:
|
||
|
|
||
|
```console
|
||
4 years ago
|
$ cargo run
|
||
4 years ago
|
Student: Student(20, "Pedro", "Domingos")
|
||
|
Student first name: Pedro
|
||
|
Student last name: Domingos
|
||
|
Student Id: 20
|
||
4 years ago
|
$
|
||
4 years ago
|
```
|
||
2 years ago
|
|
||
|
### Notions
|
||
|
|
||
|
- [Defining a struct](https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html)
|
||
|
|
||
|
- [The Tuple Type](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html?highlight=accessing%20a%20tuple#compound-types)
|
||
|
|
||
|
- [Tuples](https://doc.rust-lang.org/rust-by-example/primitives/tuples.html)
|
||
|
|
||
|
- [Tuple Structs without Named Fields](https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html?highlight=tuple#using-tuple-structs-without-named-fields-to-create-different-types)
|
||
|
|
||
|
- [Adding Useful Functionality with Derived Traits](https://doc.rust-lang.org/stable/book/ch05-02-example-structs.html?highlight=debug%20deriv#adding-useful-functionality-with-derived-traits)
|
||
|
|
||
|
- [Chapter 7](https://doc.rust-lang.org/stable/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html)
|