From c09d5f585a1298f591e9096eaa8f760a7a3b8e5d Mon Sep 17 00:00:00 2001 From: Ed Cragg Date: Sun, 5 Dec 2021 13:55:53 +0000 Subject: [PATCH] Get a hashmap, odd borrowing Compiling aoc0521 v0.1.0 (/home/ed/git/aoc-2021/05) warning: cannot borrow `points` as mutable because it is also borrowed as immutable --> src/main.rs:85:32 | 84 | match points.get(&point) { | ------ immutable borrow occurs here 85 | Some(count) => points.insert(point, count+1), | ^^^^^^ ----- immutable borrow later used here | | | mutable borrow occurs here | = note: `#[warn(mutable_borrow_reservation_conflict)]` on by default = warning: this borrowing pattern was not meant to be accepted, and may become a hard error in the future = note: for more information, see issue #59159 warning: `aoc0521` (bin "aoc0521") generated 1 warning Finished dev [unoptimized + debuginfo] target(s) in 1.19s Running `target/debug/aoc0521` [ Line { start: Point { x: 0, y: 9, }, end: Point { x: 5, y: 9, }, }, Line { start: Point { x: 8, y: 0, }, end: Point { x: 0, y: 8, }, }, Line { start: Point { x: 9, y: 4, }, end: Point { x: 3, y: 4, }, }, Line { start: Point { x: 2, y: 2, }, end: Point { x: 2, y: 1, }, }, Line { start: Point { x: 7, y: 0, }, end: Point { x: 7, y: 4, }, }, Line { start: Point { x: 6, y: 4, }, end: Point { x: 2, y: 0, }, }, Line { start: Point { x: 0, y: 9, }, end: Point { x: 2, y: 9, }, }, Line { start: Point { x: 3, y: 4, }, end: Point { x: 1, y: 4, }, }, Line { start: Point { x: 0, y: 0, }, end: Point { x: 8, y: 8, }, }, Line { start: Point { x: 5, y: 5, }, end: Point { x: 8, y: 2, }, }, ] Line { start: Point { x: 0, y: 9 }, end: Point { x: 5, y: 9 } } dx=5 dy=0 n=5 ix=1 iy=0 Line { start: Point { x: 8, y: 0 }, end: Point { x: 0, y: 8 } } dx=-8 dy=8 n=8 ix=1 iy=-1 Line { start: Point { x: 9, y: 4 }, end: Point { x: 3, y: 4 } } dx=-6 dy=0 n=6 ix=1 iy=0 Line { start: Point { x: 2, y: 2 }, end: Point { x: 2, y: 1 } } dx=0 dy=-1 n=1 ix=0 iy=1 Line { start: Point { x: 7, y: 0 }, end: Point { x: 7, y: 4 } } dx=0 dy=4 n=4 ix=0 iy=1 Line { start: Point { x: 6, y: 4 }, end: Point { x: 2, y: 0 } } dx=-4 dy=-4 n=4 ix=1 iy=1 Line { start: Point { x: 0, y: 9 }, end: Point { x: 2, y: 9 } } dx=2 dy=0 n=2 ix=1 iy=0 Line { start: Point { x: 3, y: 4 }, end: Point { x: 1, y: 4 } } dx=-2 dy=0 n=2 ix=1 iy=0 Line { start: Point { x: 0, y: 0 }, end: Point { x: 8, y: 8 } } dx=8 dy=8 n=8 ix=1 iy=1 Line { start: Point { x: 5, y: 5 }, end: Point { x: 8, y: 2 } } dx=3 dy=-3 n=3 ix=1 iy=-1 {Point { x: 8, y: 0 }: 1, Point { x: 8, y: 8 }: 1, Point { x: 6, y: 6 }: 1, Point { x: 10, y: 4 }: 1, Point { x: 7, y: 2 }: 1, Point { x: 2, y: 3 }: 1, Point { x: 4, y: 4 }: 2, Point { x: 11, y: 4 }: 1, Point { x: 7, y: 0 }: 1, Point { x: 7, y: 5 }: 1, Point { x: 12, y: -4 }: 1, Point { x: 3, y: 4 }: 1, Point { x: 7, y: 7 }: 1, Point { x: 10, y: -2 }: 1, Point { x: 1, y: 1 }: 1, Point { x: 8, y: 2 }: 1, Point { x: 11, y: -3 }: 1, Point { x: 2, y: 2 }: 2, Point { x: 7, y: 1 }: 1, Point { x: 7, y: 3 }: 2, Point { x: 6, y: 4 }: 2, Point { x: 9, y: 7 }: 1, Point { x: 13, y: 4 }: 1, Point { x: 3, y: 3 }: 1, Point { x: 15, y: -7 }: 1, Point { x: 2, y: 9 }: 2, Point { x: 10, y: 8 }: 1, Point { x: 3, y: 9 }: 1, Point { x: 9, y: -1 }: 1, Point { x: 13, y: -5 }: 1, Point { x: 5, y: 9 }: 1, Point { x: 5, y: 4 }: 1, Point { x: 4, y: 9 }: 1, Point { x: 16, y: -8 }: 1, Point { x: 9, y: 4 }: 1, Point { x: 12, y: 4 }: 1, Point { x: 0, y: 9 }: 2, Point { x: 14, y: -6 }: 1, Point { x: 15, y: 4 }: 1, Point { x: 8, y: 6 }: 1, Point { x: 5, y: 5 }: 2, Point { x: 7, y: 4 }: 1, Point { x: 1, y: 9 }: 2, Point { x: 14, y: 4 }: 1, Point { x: 0, y: 0 }: 1} --- 05/src/main.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/05/src/main.rs b/05/src/main.rs index 4f613e1..b298ff5 100644 --- a/05/src/main.rs +++ b/05/src/main.rs @@ -67,7 +67,7 @@ fn main() { } fn run(i: &str) { - let points: HashMap = HashMap::new(); + let mut points: HashMap = HashMap::new(); let lines: Vec = i .lines() @@ -78,15 +78,15 @@ fn run(i: &str) { for line in lines { println!("{:?}", line); - println!("{:?}", line.cast()); - - line - .cast() - .for_each(|point| { - match points.get(point) { - Some(count) => points.insert(*point, count+1), - None => points.insert(*point, 1), - }; - }); + //println!("{:?}", line.cast()); + + for point in line.cast() { + match points.get(&point) { + Some(count) => points.insert(point, count+1), + None => points.insert(point, 1), + }; + }; } + + println!("{:?}", points); }