#[non_exhaustive] attribute constraints how outside code behaves in that struct or enum variant, specifically it disallows:

  • implicit constructors
#[non_exhaustive]
struct Point {
	x: f32,
	y: f32
}
 
// outside the crate, this is not allowed
Point {x: 10.0, y: 20.0};
  • non-exhaustive pattern matches
enum Point {
	2D,
	3D
}
 
// outside the crate, this is not allowed
match point {
	2D => print!("two-dimensional"),
	3D => print!("three-dimensional"),
	// would compile with `_ => {},`
}

Useful when you suspect you will modify a particular type in the future.

See also

References

  • J. Gjengset, Rust for Rustaceans: Idiomatic Programming for Experienced Developers. No Starch Press, 2021.