<h1id="privacy-for-extensibility"><aclass="header"href="#privacy-for-extensibility">Privacy for extensibility</a></h1>
<h1id="non_exhaustive-and-private-fields-for-extensibility"><aclass="header"href="#non_exhaustive-and-private-fields-for-extensibility"><code>#[non_exhaustive]</code> and private fields for extensibility</a></h1>
// .. required because this variant is non-exhaustive as well
a::AdmitMoreVariants::VariantC { a, .. } => println!("it's a c"),
// The wildcard match is required because more variants may be
// added in the future
_ => println!("it's a new variant")
}
}
<spanclass="boring">}
</span></code></pre></pre>
<h2id="alternative-private-fields-for-structs"><aclass="header"href="#alternative-private-fields-for-structs">Alternative: <code>Private fields</code> for structs</a></h2>
<p><code>#[non_exhaustive]</code> only works across crate boundaries.
Within a crate, the private field method may be used.</p>
<p>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 fields and use <code>..</code> 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.</p>
pattern.
The client could name some fields and use <code>..</code> 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.</p>
<p>The downside of this approach is that you might need to add an otherwise unneeded
field to the struct. You can use the <code>()</code> type so that there is no runtime overhead
and prepend <code>_</code> to the field name to avoid the unused field warning.</p>
<p>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 <code>_</code> wildcard
pattern. A common way to implement this instead is using the <ahref="https://doc.rust-lang.org/reference/attributes/type_system.html"><code>#[non_exhaustive]</code></a>
attribute.</p>
field to the struct.
You can use the <code>()</code> type so that there is no runtime overhead and prepend <code>_</code> to
the field name to avoid the unused field warning.</p>
<li><ahref="https://github.com/rust-lang/rfcs/blob/master/text/2008-non-exhaustive.md">RFC introducing #[non_exhaustive] attribute for enums and structs</a></li>
<divstyle="break-before: page; page-break-before: always;"></div><h1id="privacy-for-extensibility"><aclass="header"href="#privacy-for-extensibility">Privacy for extensibility</a></h1>
<divstyle="break-before: page; page-break-before: always;"></div><h1id="non_exhaustive-and-private-fields-for-extensibility"><aclass="header"href="#non_exhaustive-and-private-fields-for-extensibility"><code>#[non_exhaustive]</code> and private fields for extensibility</a></h1>
// .. required because this variant is non-exhaustive as well
a::AdmitMoreVariants::VariantC { a, .. } => println!("it's a c"),
// The wildcard match is required because more variants may be
// added in the future
_ => println!("it's a new variant")
}
}
<spanclass="boring">}
</span></code></pre></pre>
<h2id="alternative-private-fields-for-structs"><aclass="header"href="#alternative-private-fields-for-structs">Alternative: <code>Private fields</code> for structs</a></h2>
<p><code>#[non_exhaustive]</code> only works across crate boundaries.
Within a crate, the private field method may be used.</p>
<p>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 fields and use <code>..</code> 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.</p>
pattern.
The client could name some fields and use <code>..</code> 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.</p>
<p>The downside of this approach is that you might need to add an otherwise unneeded
field to the struct. You can use the <code>()</code> type so that there is no runtime overhead
and prepend <code>_</code> to the field name to avoid the unused field warning.</p>
<p>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 <code>_</code> wildcard
pattern. A common way to implement this instead is using the <ahref="https://doc.rust-lang.org/reference/attributes/type_system.html"><code>#[non_exhaustive]</code></a>
attribute.</p>
field to the struct.
You can use the <code>()</code> type so that there is no runtime overhead and prepend <code>_</code> to
the field name to avoid the unused field warning.</p>
<li><ahref="https://github.com/rust-lang/rfcs/blob/master/text/2008-non-exhaustive.md">RFC introducing #[non_exhaustive] attribute for enums and structs</a></li>
<li><ahref="https://doc.rust-lang.org/book/ch19-04-advanced-types.html?highlight=newtype#using-the-newtype-pattern-for-type-safety-and-abstraction">Advanced Types in the book</a></li>
<li><ahref="https://wiki.haskell.org/Newtype">Newtypes in Haskell</a></li>
@ -1872,7 +1950,7 @@ is shorter than the lifetime of <code>self</code>.</p>
<p>Note that implementing <code>Deref</code> is not a core part of this pattern, it only makes
using the guard object more ergonomic. Implementing a <code>get</code> method on the guard
<li><ahref="https://web.archive.org/web/20210104103100/https://doc.rust-lang.org/1.12.0/style/ownership/builders.html">Description in the style guide</a></li>
<li><ahref="https://crates.io/crates/derive_builder">derive_builder</a>, a crate for automatically
@ -2325,7 +2403,7 @@ expensive.</p>
the original data structure, and we don't need to clone unchanged nodes. However,
they are less ergonomic to use and mean that the data structures cannot be
<ahref="https://rust-lang.github.io/rust-clippy/master/index.html#map_clone">3</a> or <ahref="https://rust-lang.github.io/rust-clippy/master/index.html#clone_double_ref">4</a>.</p>
<p>This pattern is used throughout the standard library:</p>
<ul>
<li><code>Vec<u8></code> can be cast from a String, unlike every other type of <code>Vec<T></code>.<supclass="footnote-reference"><ahref="#1">1</a></sup></li>