Wrote program for Day 2
This commit is contained in:
parent
4f0b2678c2
commit
042f3bf7fa
6
Week-01/Day-02_Finding-Nemo/finding_nemo/Cargo.toml
Normal file
6
Week-01/Day-02_Finding-Nemo/finding_nemo/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "finding_nemo"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
29
Week-01/Day-02_Finding-Nemo/finding_nemo/src/main.rs
Normal file
29
Week-01/Day-02_Finding-Nemo/finding_nemo/src/main.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
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 :("),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user