mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
Change type from u32 to u8 to match IPv4 spec
Each segment of an ipv4 address can only be 0-255, so each of the segments only need to be u8s. If, however, this was done to not have to explain the difference between u32 and u8, or if this would be distracting from the point about enums trying to be made here, I would be fine reverting this change.
This commit is contained in:
parent
5b6fb9d2c4
commit
7027c996c9
@ -82,12 +82,12 @@ We can attach data to each variant of the enum directly. No need for an extra
|
||||
struct. But beyond that, this approach is better than using a struct alongside
|
||||
our enum because we can attatch different kinds of data to each variant.
|
||||
Imagine that instead of a `String`, we would prefer to store a `V4` as its four
|
||||
individual components, while leaving the `V6` variant as a `String`. With our
|
||||
individual components while leaving the `V6` variant as a `String`. With our
|
||||
struct, we’d be stuck. But enums deal with this case with ease:
|
||||
|
||||
```rust
|
||||
enum IpAddr {
|
||||
V4(u32, u32, u32, u32),
|
||||
V4(u8, u8, u8, u8),
|
||||
V6(String),
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user