diff --git a/rust/structures/mergeable_heap.rs b/rust/structures/mergeable_heap.rs index c205a40a..1d3256b5 100644 --- a/rust/structures/mergeable_heap.rs +++ b/rust/structures/mergeable_heap.rs @@ -15,23 +15,22 @@ impl Heap { })) } - fn merge(a: Option>>, b: Option>>) -> Option>> { + fn merge(mut a: Option>>, mut b: Option>>) -> Option>> { if a.is_none() { return b; } if b.is_none() { return a; } - let mut ra = a.unwrap(); - let mut rb = b.unwrap(); - if ra.value > rb.value { - swap(&mut ra, &mut rb); + if a.as_ref()?.value > b.as_ref()?.value { + swap(&mut a, &mut b); } + let mut ha = a?; if rand::random() { - swap(&mut ra.left, &mut ra.right); + swap(&mut ha.left, &mut ha.right); } - ra.left = Self::merge(ra.left, Some(rb)); - Some(ra) + ha.left = Self::merge(ha.left, b); + Some(ha) } fn remove_min(heap: Option>>) -> (Option>>, V) {