Skip to content

Commit

Permalink
Merge pull request rust-unofficial#32 from xfix/patch-1
Browse files Browse the repository at this point in the history
Prepending _ to field name avoids unused field warning
  • Loading branch information
lambda-fairy authored Oct 9, 2016
2 parents 283e410 + 6af4003 commit 26e27cd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion idioms/priv-extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 26e27cd

Please sign in to comment.