-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10-options.rs
56 lines (49 loc) · 1.69 KB
/
10-options.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// -----------------------------------------------------------------------------
// Atelier Rust
// 2022-11-19 - 16h30h
// Animateur: Axel (darnuria) && Aurelia
//
// 09-options.rs
//
// Dans cet exercice on découvre la joie des types options et match! ;p
//
// Objectifs pédagogiques:
//
// - Rappels sur les structures
// - Type Option
// - Manipulation avec match basique
//
// /!\ Quand vous verrez les symboles: `???`, il s'agit de code à
// compléter soi-même c'est normal que Rust indique une erreur! :)
// -----------------------------------------------------------------------------
// Etapes:
//
// 1. Faire compiler le code ;)
// 2. S'inspirer de l'exo 08 pour construire un user depuis une saisie clavier
// depuis la stdin
/// Structure pour representer un utilisateur et optionnellement le nom de son animal.
struct User {
firstname: String,
lastname: String,
petname: Option<String>,
}
// TODO Corriger la ligne çi dessous
impl ??? {
/// Construct a User with a optional pet.
/// Take by move for simplicity.
fn new(firstname: String, lastname: String, petname: Option<???>) -> User { // <- Fix me
User { ???, lastname, ??? }
}
}
fn main() {
// Alyx form half-life games
let alyx = User::new("Alyx".to_string(), "Vance".to_string(), Some("Dog".to_string()));
// ⬆
// `to_string()` sert a transformer une &str
// en String on vera plus tard pourquoi
let petname = match alyx.petname {
None => ", I don't own a pet".to_string(),
Some(???) => format!(", my pet name is: {}", ???),
};
println!("Hi I am {} {}{}.", alyx.firstname, alyx.lastname, petname);
}