File tree 3 files changed +43
-2
lines changed
3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -4,3 +4,4 @@ version = "0.1.0"
4
4
edition = " 2021"
5
5
6
6
[dependencies ]
7
+ rand = " 0.8.5"
Original file line number Diff line number Diff line change 1
1
# learning-rust
2
- Repo pour apprendre Ă manier Rust
2
+
3
+ Test au fur et Ă mesure de fonctions / programmes Ă©crits en Rust pour se familiariser avec ce langage.
4
+
5
+ Sources :
6
+
7
+ doc.rust-lang.org/book
Original file line number Diff line number Diff line change
1
+ use std:: io; //Librairie input/output io dans std
2
+ use rand:: Rng ; //Librairie random dans rand
3
+ use std:: cmp:: Ordering ; //Librairie comparaison dans std
4
+
1
5
fn main ( ) {
2
- println ! ( "Hello, world!" ) ;
6
+ hello_world ( ) ;
7
+ devinette ( ) ;
8
+ }
9
+
10
+ fn devinette ( ) {
11
+
12
+ let secret_number = rand:: thread_rng ( ) . gen_range ( 1 ..=100 ) ;
13
+ println ! ( "Le chiffre secret est : {}" , secret_number) ;
14
+
15
+ println ! ( "Devine le chiffre que j'ai décidé ! : " ) ;
16
+
17
+ let mut guess = String :: new ( ) ;
18
+ //let ça crée un tuple par défaut, inchangeable, sauf si on rajoute mut dessus
19
+ //String::new ça signifie que on crée un nouveau string vide (UTF-8)
20
+
21
+ io:: stdin ( ) //fonction stdin incluse dans io, avec des méthodes en dessous
22
+ . read_line ( & mut guess)
23
+ . expect ( "Echec de lecture..." ) ;
24
+
25
+ let guess: u32 = guess. trim ( ) . parse ( ) . expect ( "Tu n'as pas entré un nombre !" ) ;
26
+
27
+ match guess. cmp ( & secret_number) {
28
+ Ordering :: Less => println ! ( "Trop petit !" ) ,
29
+ Ordering :: Greater => println ! ( "Trop grand !" ) ,
30
+ Ordering :: Equal => println ! ( "Félicitations, tu as gagné !" ) ,
31
+ }
32
+
3
33
std:: io:: stdin ( ) . read_line ( & mut String :: new ( ) ) . unwrap ( ) ;
4
34
}
35
+
36
+ fn hello_world ( ) {
37
+ println ! ( "Hello, world!\n " ) ;
38
+ std:: io:: stdin ( ) . read_line ( & mut String :: new ( ) ) . unwrap ( ) ; //pour attendre une touche pressée avant de poursuivre
39
+ }
You can’t perform that action at this time.
0 commit comments