Merge pull request #32 from xfix/patch-1

Prepending _ to field name avoids unused field warning
pull/37/head
Chris Wong 8 years ago committed by GitHub
commit 26e27cd307

@ -30,6 +30,6 @@ fn main(s: a::S) {
Adding a field to a struct is a mostly backwards compatible change. However, if a client uses a pattern to deconstruct a struct instance, they might name all the fields in the struct and adding a new one would break that pattern. The client could name some of the fields and use `..` in the pattern, in which case adding another field is backwards compatible. Making at least one of the struct's fields private forces clients to use the latter form of patterns, ensuring that the struct is future-proof.
The downside of this approach is that you might need to add an otherwise unneeded field to the struct. You can use the `()` type so that there is no runtime overhead and can add the annotation `#[allow(dead_code)]` to the struct to avoid the unused field warning.
The downside of this approach is that you might need to add an otherwise unneeded field to the struct. You can use the `()` type so that there is no runtime overhead and prepend `_` to the field name to avoid the unused field warning.
If Rust allowed private variants of enums, we could use the same trick to make adding a variant to an enum backwards compatible. The problem there is exhaustive match expressions. A private variant would force clients to have a `_` wildcard pattern.

Loading…
Cancel
Save