last day (15 days later) » 

4:58 PM
4
A: How do I match enum values with an integer?

ShepmasterSince Rust 1.34, I recommend implementing TryFrom: use std::convert::TryFrom; impl TryFrom<i32> for MyEnum { type Error = (); fn try_from(v: i32) -> Result<Self, Self::Error> { match v { x if x == MyEnum::A as i32 => Ok(MyEnum::A), x if x == MyEnum::B as...

 
I can't understand, looks like magic. x has type i32 right? Then how does Rust know that it must be matched against MyEnum (and not some other enum that also implemented the trait) variants?
 
@NurbolAlpysbayev it must use the implementation of TryInto that corresponds to MyEnum because that's the type of the value that is used in the match arms (Ok(MyEnum::A)). How does Rust infer resultant types from From::<>::from()?; How does Rust's type inference work across multiple statements?
 
I'll read the links, but in short, Rust looks at the first arm and makes sure the following arms are with the same enum type (and then also ensures exhaustiveness), am I right? Looks like this is the case.
Unfortunately, I've found that this solution may have a disatvantage in that exhaustiveness is not checked in the try_from function. So basically it's not reliable.
 
@NurbolAlpysbayev you seem to be using multiple (non-standard?) meanings for the word "exhaustive". You may be interested in How to ensure every enum variant can be returned from a specific function at compile time?.
 
I believe I use the meaning from the Rust Book. Regarding the link, I am not sure how that can be used. Anyway, the top voted answer seems to work. I just wish there'd be a zero-dependency, reliable (with exhaustiveness check) and preferably concise solution.
 
4:58 PM
To be clear, the derived implementation of FromPrimitive looks the same as the TryFrom implementation in this answer (using Option instead of Result, but otherwise...)
 
I may really miss something, but I tested that, and no, it doesn't look the same in terms of reliability. Just remove x if x == MyEnum::B as i32 => Ok(MyEnum::B) line and you will get nothing but a "dead code" warning. I posted the link to the playground somewhere above. I really wished I could use some "standard", reliable approach, but this is not it, probably.
 
Yes, that's the implementation of TryFrom and FromPrimitive
When you use #[derive(FromPrimitive)] it automatically writes code like
impl FromPrimitive for MyEnum {
    fn from_primitive(v: i32) -> Option<Self> {
        match v {
            x if x == MyEnum::A as i32 => Some(MyEnum::A),
            x if x == MyEnum::B as i32 => Some(MyEnum::B),
            x if x == MyEnum::C as i32 => Some(MyEnum::C),
            _ => None,
        }
    }
}
 
Ahh. So all that FromPrimitive does, is writing boilerplate code for you or somthing?
 
11
Q: What exactly does '#[derive(Debug)]' mean in Rust?

AmaniWhat exactly does #[derive(Debug)] mean? Does it have something to do with 'a? For example: #[derive(Debug)] struct Person<'a> { name: &'a str, age: u8 }

yes.
well, derive(FromPrimitive)
 
But that's quite reliable if compared to manual implementation, right?
 
5:02 PM
Depends. It's something that some people are worried about, but I'm not one of them.
I'd have written some kind of test that would cause that code to be written
 
I see. I am one of those people who worry about these kind of things :-) Paranoidal
 
Which is why I linked to stackoverflow.com/q/58715081/155423
Which is someone else with the same concern
in a more generic example
The answers there basically boil down to using macros (another type of code generation) to prevent the problem
 
You are reasoning from the point of view of a very experienced advanced Rust user, and I have a point of view of just a newbie :-) So I guess this is why we have different preferred approaches :-)
Anyway, thank you so much for your time and for showing me a bit of the other side of how things work :-)
 
I don't think this particular opinion of mine is based on Rust experience, just that I like writing tests.
No problem!
 
Yeah, this is exactly the thing - you are like writing tests, and I hate them with passion :) I prefer that compiler takes as much as possible pain from me, including validation that things work
 
5:12 PM
Sure, but in your case there isn't really any (obvious) check. You are relying on the implementation of the derive to do the right thing.
Hopefully they have tests
 

last day (15 days later) »