Skip to content

Commit f3d6a0e

Browse files
committed
feat: add new coding challenge to have fun
1 parent 4d535ce commit f3d6a0e

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

multimap/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

multimap/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "multimap"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

multimap/phonebook.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 Hugo
2+
1 Hugo
3+
2 Hello
4+
2 Bye
5+
10 Find

multimap/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::{collections::HashMap, error::Error, fs};
2+
3+
fn main() -> Result<(), Box<dyn Error>> {
4+
let content = fs::read_to_string("phonebook.txt")?;
5+
let mut store: HashMap<i32, Vec<&str>> = HashMap::new();
6+
7+
for line in content.lines() {
8+
let (number, name) = line
9+
.split_once(' ')
10+
.ok_or_else(|| format!("malformed line: {line:?}"))?;
11+
let key: i32 = number.parse()?;
12+
store.entry(key).or_default().push(name);
13+
14+
println!("{name} -> {number}");
15+
}
16+
17+
println!("{:?}", store.get(&99));
18+
println!("{:?}", store.get(&1));
19+
println!("{:?}", store.get(&2));
20+
21+
Ok(())
22+
}

0 commit comments

Comments
 (0)