Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In rust this would simply be

   enum ConnectionStste {
        Connected(u32),
        Disconnected
    }
 help



or

   struct Connected { peer_ip: u32 }
   struct Disconnected;
   struct Connection<State> { state: State }

Wouldn’t peer: Option<int32> be enough to represent this?

None = disconnected


Personally I don’t like using nil to represent specific states in my domain. I try to only judiciously use it to represent the lack of knowledge about some state, like if I need to reach out to some external service I don’t control, that may or may not give me an answer for my query (and conversely, I don’t like putting “unknown” cases into my enums).

Yes, it'd probably be compiled into very machine code. However defining an enum for it puts the intention into the source code more explicitly, so that's considered the more idiomatic way. You'd probably have a `fn peer_id(&self) -> Option<Inet4Addr>` accessor that you'd use in the case where you'd want the Option.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: