-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆕 Ajout de code, test de certains méthodes
- random nombre Ă entrer et Ă comparer, et dire si c'est plus grand ou plus petit
- Loading branch information
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
rand = "0.8.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# learning-rust | ||
Repo pour apprendre Ă manier Rust | ||
|
||
Test au fur et Ă mesure de fonctions / programmes Ă©crits en Rust pour se familiariser avec ce langage. | ||
|
||
Sources : | ||
|
||
doc.rust-lang.org/book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
use std::io; //Librairie input/output io dans std | ||
use rand::Rng; //Librairie random dans rand | ||
use std::cmp::Ordering; //Librairie comparaison dans std | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
hello_world(); | ||
devinette(); | ||
} | ||
|
||
fn devinette() { | ||
|
||
let secret_number = rand::thread_rng().gen_range(1..=100); | ||
println!("Le chiffre secret est : {}", secret_number); | ||
|
||
println!("Devine le chiffre que j'ai décidé ! : "); | ||
|
||
let mut guess = String::new(); | ||
//let ça crée un tuple par défaut, inchangeable, sauf si on rajoute mut dessus | ||
//String::new ça signifie que on crée un nouveau string vide (UTF-8) | ||
|
||
io::stdin() //fonction stdin incluse dans io, avec des méthodes en dessous | ||
.read_line(&mut guess) | ||
.expect("Echec de lecture..."); | ||
|
||
let guess: u32 = guess.trim().parse().expect("Tu n'as pas entré un nombre !"); | ||
|
||
match guess.cmp(&secret_number) { | ||
Ordering::Less => println!("Trop petit !"), | ||
Ordering::Greater => println!("Trop grand !"), | ||
Ordering::Equal => println!("Félicitations, tu as gagné !"), | ||
} | ||
|
||
std::io::stdin().read_line(&mut String::new()).unwrap(); | ||
} | ||
|
||
fn hello_world() { | ||
println!("Hello, world!\n"); | ||
std::io::stdin().read_line(&mut String::new()).unwrap(); //pour attendre une touche pressée avant de poursuivre | ||
} |