I'm trying to learn Rust then I make a little game to practice.
The goal of the game is simple. Chose a letter and if the letter is not in the word, you lose a life. You must discover the word.
There is my code:
use std::io::{stdin};
use rand::Rng;
fn main() {
let array_mot_pendu = ["trotinette", "pendu", "ravioli"];
let mut rng = rand::thread_rng();
let mot_a_deviner = array_mot_pendu[rng.gen_range(0..array_mot_pendu.len())];
println!("{}", mot_a_deviner);
let mut d = ['_'; 5];
d[0] = mot_a_deviner.chars().nth(0).unwrap();
println!("{:?}", d);
let ch = mot_a_deviner.chars().nth(0).unwrap();
println!("Bienvenue dans ce pendu! Vous avez 5 vies pour deviner le mot affiché. Si vous vous trompez vous perdez une vie.");
let mut input_string = String::new();
stdin().read_line(&mut input_string)
.ok()
.expect("ERREUR");
let cha = input_string.chars().nth(0).unwrap();
println!("?{}", cha);
loop {
if input_string.len() == 1 {
println!("ah")
// if input_string.is_alphabetic() {
// break
// }
// }
// else:
// println!("Erreur, choisissez une lettre");
// let mut input_string = String::new();
}
}
}
The problem is that the length of the string is greater than two, but I can't display it. Is it like in C, or is there a "\n" behind it? I couldn't find anything on it.
When I put this
println!("{}", input_string.chars().count());
It gives me my string +2 characters
(If you have a better solution for specifying that the string must be a char better than if input_string.len() == 1
I welcome the answer.)