100-days-of-rust/Week-01/Day-02_Finding-Nemo/finding_nemo/src/main.rs

30 lines
632 B
Rust
Raw Normal View History

2024-07-27 12:59:53 +00:00
use std::io::{self, Write};
fn find_nemo(word: &str) -> Option<usize> {
for (i, word) in word.split(" ").enumerate() {
if word == "Nemo" {
return Some(i + 1);
}
}
None
}
fn main() {
let mut buffer = String::new();
print!("Insert your words: ");
io::stdout().flush().expect("Failed to flush stdout.");
io::stdin()
.read_line(&mut buffer)
.expect("Error while trying to read from stdin.");
let nemo = find_nemo(&buffer);
match nemo {
Some(x) => println!("I found Nemo at {}!", x),
None => println!("I can't find Nemo :("),
}
}