Skip to content
Snippets Groups Projects
Unverified Commit 179d72c0 authored by Vanille-N's avatar Vanille-N
Browse files

intro aux types sommes

parent 4df7a097
No related branches found
No related tags found
No related merge requests found
......@@ -592,13 +592,42 @@ fn main() {
// soit plusieurs valeurs qu'on risque de confondre
\end{lstlisting}
fn print_user(user: User) {
// Afin de comparer l'enum, on utilise un pattern matching
match user.nickname {
MaybeNickname::Nickname(nickname) => println!("{}: {} aka {}", user.id, user.name, nickname),
MaybeNickname::NoNickname => println!("{}: {}", user.id, user.name),
\subsection{Type somme}
Pour un type qui est l'union de plusieurs types, on utilise une \texttt{enum}.
Les variants peuvent eux-mêmes être des types produits.
\begin{lstlisting}[style=Rust, language=Rust]
enum U {
Zero,
Un(u8),
Deux(u8, char),
Trois {
n: u8,
c: char,
f: f64,
},
}
fn main() {
let u0 = U::Zero;
let u1 = U::Un(1);
let u2 = U::Deux(2, 'b');
let u3 = U::Trois {
n: 3,
c: 'c',
f: 3.0,
};
match u3 with {
U::Zero => println!("zero"),
U::Un(n) => println!("un:{}", n),
U::Deux(n, c) => println!("deux:{},{}", n, c),
U::Trois { n, c, f } => println!("trois:{},{},{}", n, c, f),
}
}
\end{lstlisting}
\newpage
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment