These are chat archives for rust-lang/rust

8th
Oct 2018
Rotem Yaari
@vmalloc
Oct 08 2018 04:22
@tanriol that works! Thanks!
Sergey Bushnyak
@sigrlami
Oct 08 2018 17:04
Is there any way to handle more appropriately something like this &result .as_ref() .unwrap() .as_ref() .unwrap() .first() .unwrap()
and remove many .as_ref combinations?
Zakarum
@omni-viral
Oct 08 2018 17:11
@sigrlami it depends on what result is
Sergey Bushnyak
@sigrlami
Oct 08 2018 17:14
@omni-viral pub fn get_values(result: &Result<Option<Vec<Node>>, error::Error>) -> &Vec<Vec<Value>> { &result .as_ref() .unwrap() .as_ref() ...
getting results from db
Zakarum
@omni-viral
Oct 08 2018 18:01
What you return if there is no vec?
Ah. Panicking. Right
Nah, that is pretty much all you can do. as_refing and unwrapping
Sergey Bushnyak
@sigrlami
Oct 08 2018 18:03
I was wondering if there is a nicer way to do this, sometimes sequence is up to 20loc and looks like code smell
maybe map over in some way?
Zakarum
@omni-viral
Oct 08 2018 18:04
unwraping function argument looks like code smell already
It's ok for prototyping
But in prod you'd better handle those errors
Sergey Bushnyak
@sigrlami
Oct 08 2018 18:07
that's because of work with db, what about ? sign instead unwrap. But you'll end up in spaghetti of error handlers . Not sure how to approach it idiomatically so it looks nice and readable without taking too much boilerplate
Zakarum
@omni-viral
Oct 08 2018 18:08
Usually you end up with 90% functions returning Result
Sergey Bushnyak
@sigrlami
Oct 08 2018 18:08
maybe some kind of generalized iterator with error handling?
Zakarum
@omni-viral
Oct 08 2018 18:08
And this is idiomatic
Iterator with error handling?
Sergey Bushnyak
@sigrlami
Oct 08 2018 18:18
like fn unwrapSequentially(result: &Result.., <list of accessor functions>, until specific point of structure, with higher-order functions and it will do as_ref.unwrap() as many times as needed. I'm not that good in Rust to explain it better
but you can do it in some languages
red75prime
@red75prime
Oct 08 2018 18:23
if let &Ok(Some(ref vec)) = result { &vec[0] } else { panic!("Oops") }
kraigher
@kraigher
Oct 08 2018 20:38
Is there a brief method to create a filter function (enum -> bool) from an expression such as EnumVariant1 | EnumVariant2:
for example:
enum_vec.iter().filter(EnumVariant1 | EnumVariant2)
I guess the best method I can think of is a macro that creates a match statement based on a list of enum variants
Is there already something like this in the standard library?
kraigher
@kraigher
Oct 08 2018 21:27
I ended up with this macro:
macro_rules! kinds {
    ($($kinds:pat)|+) => {|kind|
        match kind {
            $($kinds)|+ => true,
            _ => false,
        }
    }
}