Use non-exhaustive attribute to indicate that enum may have more variants in future.
1
2
3
4
5
#[non_exhaustive]
enum Event{
Error,
Completed(String)
}
If such enum is used in match pattern, it is required to handle ‘_‘ pattern.
1
2
3
4
5
6
7
8
9
10
11
12
let event = Event::Error;
match event {
Event::Error => {
println!("error")
}
Event::Completed(s) => {
println!("completed {}", s)
}
_ => {
println!("unknown")
}
}
How is this useful?
If you are a maintainer of a library and you’d expect to add more variants in the future.
This approach helps you to build a library with non-breaking code changes for library users.