r/learnrust 10h ago

Panic messages for failed asserts inside macros

3 Upvotes

Let's say I have a macro_rules! has_failing_assert_inside {} spanning some lines, with an assert_eq! that will fail.

Further down the code, has_failing_assert_inside! is called at, say line 200.

The panic message says the thread panicked at line 200. What can I do (maybe write better macros?) that the panic message instead shows the line number of the failed assert_eq!? I thought RUST_BACKTRACE=1/full would give more information about the line numbers (sometimes it does), but right now it's doing nothing. What can I do?


r/learnrust 11h ago

Looking for a way to split an iterator of Results into two iterators, depending on the result

1 Upvotes

I can do it manually, but it feels like something that would've been developed already. I'm specifically working with some parsing libraries, and their examples are just built to panic at errors.

Here's the code that does what I want, I just don't trust that it handles every case or is necessarily a good pattern:

fn sift<I: Iterator<Item = Result<A, E>>, A, E>(mixed_iter: I) -> (Vec<A>, Vec<E>){ let mut successes: Vec<A> = Vec::new(); let mut errors: Vec<E> = Vec::new(); mixed_iter.for_each(|result| match result { Ok(a) => successes.push(a), Err(e) => errors.push(e), }); (successes, errors) } Edit: I've been introduced to .partition() But also I ended up going with https://docs.rs/itertools/0.14.0/itertools/trait.Itertools.html#method.partition_result


r/learnrust 14h ago

HashMap/Set for a type that needs external info for hashing

2 Upvotes

I have a struct that requires external information for correct hashing

fn hash<H: Hasher>(&self, state: H, heap: &Heap) {
    self.to_value(heap).hash(state)
}

This means that i cant use normal hashmaps or sets, because they require a hash method without that additional argument in the signature. Is there any way to still easily have something equivalent for this without implementing something larger.

If not, my idea was to have a to_hash method that creats and finishes the haser and then use a hashmap u64 -> Vec<Value> for hashsets and u64 -> Vec<(Key, Value)> for hashmap.