public/subjects/tuples_refs/README.md

60 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2022-05-24 14:06:02 +00:00
## tuples_refs
2020-12-25 17:35:42 +00:00
### Instructions
- Define a tuple `struct` to represent a `Student`. Each is identified by an id of type `u32`, their first name and last name.
2020-12-25 17:35:42 +00:00
2021-02-03 20:01:41 +00:00
- Then define three **functions** to return the id, first name and last name.
2020-12-25 17:35:42 +00:00
```rust
pub fn id(student: &Student) -> u32 {
2020-12-25 17:35:42 +00:00
}
pub fn first_name(student: &Student) -> String {
}
pub fn last_name(student: &Student) -> String {
}
```
### Usage
2021-02-03 19:53:35 +00:00
Here is a program to test your functions
2020-12-25 17:35:42 +00:00
```rust
2022-05-24 14:06:02 +00:00
use tuples_refs::*;
2020-12-25 17:35:42 +00:00
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
$ cargo run
2020-12-25 17:35:42 +00:00
Student: Student(20, "Pedro", "Domingos")
Student first name: Pedro
Student last name: Domingos
Student Id: 20
$
2020-12-25 17:35:42 +00:00
```
2022-05-22 12:42:16 +00:00
### 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)