Learn enums in Rust using simple step by step examples.

Example 1: Simple Enum Example

Beginners introduction to enum using simple example.

Step 1: Create Project

The first step is to create a Rust project .

Creating a Project Directory

Open a terminal and enter the following commands to make a projects directory and an inner directory for this example within that projects directory.

For Linux, macOS, and PowerShell on Windows, enter this:

$ mkdir ~/projects
$ cd ~/projects
$ mkdir your_example_name
$ cd your_example_name

For Windows CMD, enter this:

> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir your_example_name
> cd your_example_name

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Here’s how you define an enum:

enum Shape {
    Circle(Point, f64),
    Rectangle(Point, Point)
}

Here’s an example of an enum of structs:

enum StructShape {
    StructCircle { center: Point, radius: f64 },
    StructRectangle { top_left: Point, bottom_right: Point }
}

Here’s the full code:

main.rs

#![allow(dead_code)]

use std::f64::consts::PI;

#[derive(Clone, Copy)]
struct Point {
    x: f64,
    y: f64
}

/*
 * Enum variant
 */
enum Shape {
    Circle(Point, f64),
    Rectangle(Point, Point)
}

fn area(sh: Shape) -> f64 {
    match sh {
        Shape::Circle(_, size) => PI * size * size,
        Shape::Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y - y2)
    }
}

/*
 * Enum of structs
 */
enum StructShape {
    StructCircle { center: Point, radius: f64 },
    StructRectangle { top_left: Point, bottom_right: Point }
}

/**
 * Compute area of a Shape
 */
fn struct_area(sh: StructShape) -> f64 {
    match sh {
        StructShape::StructCircle { radius, .. } => PI * radius * radius,
        StructShape::StructRectangle { top_left, bottom_right } => {
            (bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
        }
    }
}

/**
 * Main program
 */
fn main() {
    let top_left = Point { x: 0.0, y: 1.0 };
    let bottom_right = Point { x: 1.0, y: 0.0 };

    /*
     * Enum variant
     */
    let my_circle1 = Shape::Circle(top_left, 1.0);
    let my_rectangle1 = Shape::Rectangle(top_left, bottom_right);
    let circle_area1: f64 = area(my_circle1);
    let rectangle_area1: f64 = area(my_rectangle1);
    println!("variant: circle area: {}", circle_area1);
    println!("variant: rectangle area: {}", rectangle_area1);

    /*
     * Enum of structs
     */
    let my_circle = StructShape::StructCircle { center: top_left, radius: 1.0};
    let my_rectangle = StructShape::StructRectangle { top_left: top_left, bottom_right: bottom_right};
    let circle_area: f64 = struct_area(my_circle);
    let rectangle_area: f64 = struct_area(my_rectangle);
    println!("struct: circle area: {}", circle_area);
    println!("struct: rectangle area: {}", rectangle_area);
}

If you run the code you will get the following:

variant: circle area: 3.141592653589793
variant: rectangle area: 1
struct: circle area: 3.141592653589793
struct: rectangle area: 1

Run

Save the code and run your code as follows:

Assuming our file name is main.rs, first compile it

$ rustc main.rs

then run it:

$ ./main

On Windows, enter the command .\main.exe instead of ./main:

compile:

> rustc main.rs

then run it:

> .\main.exe

Categorized in: