Prevent Breaking Code Changes in Future Releases using `non exhaustive` enums in Rust

Prevent Breaking Code Changes in Future Releases using `non exhaustive` enums in Rust

Hello everyone, recently I have come across a feature in Rust, known as non_exhaustive. It was introduced in Rust 1.40.0 . This attribute prevents source code-breaking changes in projects downstream.

Hello everyone, recently I have come across a feature in Rust, known as non_exhaustive. It was introduced in Rust 1.40.0 . This attribute prevents source code-breaking changes in projects downstream.

enums can be non-exhaustive or exhaustive, depending upon their needs. A non-exhaustive enum indicates that this enum may get new value in the future. By adding #[non_exhaustive] attribute, we can create a non-exhaustive enum which forces downstream crates to use wildcard pattern (_) in the match expression and make it non-exhaustive. Otherwise, we will get the compilation error.

Let’s see with a simple example.

  • Create a project using cargo new and define a rust module movie_genresunder this project.
  • Create a file lib.rs and add an enum for movie genres with #[non_exhaustive] attribute.
#[non_exhaustive]
pub enum Genres {
    Horror,
    Comedy,
    Romance,
}
  • In main Cargo.toml add the dependency.
movie_genres = {version = "0.1.0", path="./movie_genres"}
  • Now import movie_genres in main.rs and start testing.
extern crate movie_genres;
use movie_genres::Genres;

fn main() {
    let genres = Genres::Horror;

    match genres {
        Genres::Horror => println!("Horror Movie!!"),
        Genres::Comedy => println!("Comedy Movie!!"),
        Genres::Romance => println!("Romance Movie!!"),
    }
}
  • Now if we hit cargo run, we will get a compilation error.

  • Now add wildcard arm in match expression.
    match genres {
        Genres::Horror => println!("Horror Movie!!"),
        Genres::Comedy => println!("Comedy Movie!!"),
        Genres::Romance => println!("Romance Movie!!"),
        _ => println!("Other movie category"),
    }

rust enumerations rust programming language rust projects

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

RUST on programming language

In this blog, I am going to run around and see the different aspects of programming language rust. And talk about the concepts that it introduces that are useful for various aspects of programming.

This Rust programming language

An introduction to the Tuples data type. This Rust programming language tutorial series is aimed at easing your training step by step. Rust is a systems leve...

10 Writing steps to create a good project brief - Mobile app development

This comprehensive article covers 10 writing steps to create a good project brief/summary for mobile app development and the writing mistakes to avoid.

Benefits Of C Language Over Other Programming Languages

C Language Online Training Course; CETPA offers exclusive live project based C Language Training in Noida, Delhi NCR Lucknow, Dehradun, Roorkee. C Language Online Course also available with certification and 100% placement assistance.