Structs, enums and match in Rust

Learn Rust · Beginner · 12 min · After Borrowing and references in Rust

How do you model your own data in Rust?

  1. 1Guess

    enum Light {
        Red,
        Yellow,
        Green,
    }
    
    fn main() {
        for light in [Light::Red, Light::Yellow, Light::Green] {
            match light {
                Light::Red => println!("stop"),
                Light::Green => println!("go"),
                _ => println!("wait"),
            }
        }
    }
    
    What does this program print?
  2. 2Read

    Structs and methods

    A struct groups named fields: struct Rectangle { width: u32, height: u32 }. You make an instance by giving every field a value and read a field with a dot. Methods live in an impl block. A method whose first parameter is &self borrows the instance, so you call rect.area() and still own rect afterwards.

  3. 3Code

    A struct with a method

    1 / 2
    #[derive(Debug)]
    struct Rectangle {
        width: u32,
        height: u32,
    }
    
    impl Rectangle {
        fn area(&self) -> u32 {
            self.width * self.height
        }
    }
    
    fn main() {
        let rect = Rectangle {
            width: 30,
            height: 50,
        };
        println!("{:?}", rect);
        println!("area is {}", rect.area());
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Enums, Option and match

    An enum is a type with a fixed set of variants, and a variant can hold data. Rust has no null. A value that may be missing has the type Option<T>: Some(value) or None. match compares a value with patterns, and it is exhaustive: you must cover every variant, or use _ for the rest.

  5. 5Code

    An enum with data, and an Option

    1 / 2
    enum Shape {
        Square(f64),
        Rect { w: f64, h: f64 },
    }
    
    fn area(shape: &Shape) -> f64 {
        match shape {
            Shape::Square(side) => side * side,
            Shape::Rect { w, h } => w * h,
        }
    }
    
    fn main() {
        println!("{}", area(&Shape::Square(3.0)));
        println!("{}", area(&Shape::Rect { w: 2.0, h: 5.0 }));
        let found: Option<u32> = Some(7);
        if let Some(n) = found {
            println!("found {n}");
        }
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    enum Coin {
        Penny,
        Nickel,
        Dime,
    }
    
    fn cents(coin: &Coin) -> u32 {
        match coin {
            Coin::Penny => 1,
            Coin::Nickel => 5,
        }
    }
    
    fn main() {
        println!("{}", cents(&Coin::Penny));
        println!("{}", cents(&Coin::Nickel));
        println!("{}", cents(&Coin::Dime));
    }
    
    This program does not compile. What is the bug?
  7. 7Say it back

  8. 8Mission · 10 min

    Define an enum with three variants, one of them holding a number, and a function that matches on it. Add a struct with an impl block and a method, and print both from main. Then remove one match arm and read the error.

    cargo new shapes
    cd shapes
    cargo run

Free AI coding tools to practise with

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

Common questions

How do I define a struct in Rust?
Write the keyword struct, a name and a list of named fields with their types in braces, for example struct Rectangle { width: u32, height: u32 }. To make a value, give every field a value, and read a field with a dot. Methods go in a separate impl block, with &self as the first parameter.
What is Option in Rust and why is there no null?
Rust has no null value. When a value may be missing, its type is Option<T>, an enum with two variants: Some(value) and None. Because it is a different type from T, the compiler makes you handle the None case before you can use the value, so you cannot forget it by accident.
Why must a match be exhaustive in Rust?
Every match must cover all possible values of what it matches, or the program does not compile. The compiler even names the missing pattern. This means a forgotten case becomes a compile error rather than a bug at run time. If you only care about some cases, a final _ arm covers all the rest.

Sources

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer