Reputation: 61
I'm learning Rust. I have some programming experience in JavaScript and Python and had a good go at Haskell once.
I'm learning Enums but finding it hard to use them. Here's what I'm doing:
fn main(){
enum IpAddr {
V4(u8, u8, u8, u8),
V6(u16, u16, u16, u16, u16, u16, u16, u16),
}
impl IpAddr {
fn last_octet (&self) -> &u16 {
match &self {
IpAddr::V4(_, _, _, d) => d as &u16,
IpAddr::V6(_, _, _, _, _, _, _, h) => h,
}
}
}
let localhost4 = IpAddr::V4(127, 0, 0, 1);
let localhost6 = IpAddr::V6(0, 0, 0, 0, 0, 0, 0, 1);
println!("{}", localhost4.last_octet());
println!("{}", localhost6.last_octet());
}
So, I want to use u8 and u16 respectively for IPv4 and IPv6 to take advantage of the type system.
I realise my last_octet method can only return one type which is a bit of a limitation, so the obvious approach seems to convert my IPv4 octet to a u16 but I'm unable to find a way to do that.
Any suggestions or improvements on what I've done??
I tried my main function above and it fails on the conversion from u8 to u16.
If I try the same a simple
let u8: u8 = 7;
let u16: u16 = u8 as u16;
there is no problem. So, I'm not understanding something about Enums or methods on them.
Upvotes: 0
Views: 98
Reputation: 6651
The problem is that you're trying to return a reference to a u16
and you can't trivially cast between references to different types. The good news is that there's almost never a good reason to return a reference to an integer instead of just an integer. All the integral types in Rust are Copy
, i.e. they don't get moved around, they're just copied bit by bit because it's much cheaper and they don't own any data that could get aliased in the process. So you can just return a u16
instead of a &u16
and upcast your u8
:
impl IpAddr {
fn last_octet (&self) -> u16 {
match &self {
// note the dereferencing operator `*` here to turn a `&u8` into a `u8`
// before casting it to a `u16`:
IpAddr::V4(_, _, _, d) => *d as u16,
// here we just need to dereference:
IpAddr::V6(_, _, _, _, _, _, _, h) => *h,
}
}
}
Upvotes: 6