These are chat archives for rust-lang/rust

10th
Jan 2019
earthengine
@earthengine
Jan 10 00:46
@IMacronaut_twitter That will mean you have to write foo + &bar rather than foo + bar, which is ugly to me.
Farhan Ahmed
@IMacronaut_twitter
Jan 10 03:26

@earthengine I agree, but doing this doen’t move the value. My use case is astornomical calculations. I need to call multiple functions, one after the other, that operate on the type, Foo. Therefore, doing foo + bar will move the value when the next expression that needs the original value of foo is called.

And to avoid that, I would have to do foo.clone() + bar; which is a bit tedious.

Which one is more idomatic of Rust? Also are there better ways to do that.

earthengine
@earthengine
Jan 10 03:28
  1. If you have simple types like Foo above, you would like to derive Copy and don't worry about moveing the value
  1. If it is not the case, then you might want to implement for &Foo rather than Foo, so you have &foo + &bar which would be better.
Farhan Ahmed
@IMacronaut_twitter
Jan 10 03:31
@earthengine I forgot about the Copy trait. 🤦‍♂️ Let me try that first. If that doesn’t work, I will give the second option a shot. Thank you.
@earthengine Can you tell I’m new to Rust ;)
Farhan Ahmed
@IMacronaut_twitter
Jan 10 18:05
I need to have an enum. Each of the enum variants that have some constant value associated with them. How can I, idiomatically, do this in Rust? Is enum a valid data structure for this?
The following is what I have so far: https://paste.gg/p/insha/6a2cce1922a141b08ccaf8f14496f439
Ingvar Stepanyan
@RReverser
Jan 10 18:06
Yes, you can literally assign constant value to each variant (assuming they're integers and your enum doesn't have nested values)
enum Methods {
  VariantOne = 1,
  VariantTwo = 2,
}
Farhan Ahmed
@IMacronaut_twitter
Jan 10 18:07
Thank you! @RReverser .
Ingvar Stepanyan
@RReverser
Jan 10 18:07
To convert to number, just use as
E.g.
#[derive(Clone, Copy)]
enum Methods {
    VariantOne = 1,
    VariantTwo = 2,
}

impl Methods {
    pub fn shadow(&self) -> i32 {
        *self as i32
    }
}
Farhan Ahmed
@IMacronaut_twitter
Jan 10 18:08
I see. Thank you again. :thumbsup:
Ingvar Stepanyan
@RReverser
Jan 10 18:08
Np