0Pricing
Learn Rust Coding · Lesson

Binding with @

Capture and test.

Binding with @ is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The @ Binding Operator

The @ operator lets you test a value against a pattern and bind it to a name at the same time. You get both the check and the value.

The Problem It Solves

With a range pattern you know a value fell in range, but you lose access to the specific value. @ captures it while still testing the range.

Basic @ Binding

Here we match a number in a range and keep its exact value via id.

fn main() {
    let n = 5;
    match n {
        id @ 1..=5 => println!("got {id} in range"),
        _ => println!("out of range"),
    }
}

Without @

Compare: a plain range arm matches but cannot name the value, so you cannot print which number matched.

fn main() {
    let n = 5;
    match n {
        1..=5 => println!("in range (value unknown here)"),
        _ => println!("out of range"),
    }
}

@ With Enum Data

Bind a field while also constraining it. Here we capture the id only if it falls in a valid range.

enum Message {
    Hello { id: i32 },
}

fn main() {
    let msg = Message::Hello { id: 7 };
    match msg {
        Message::Hello { id: id @ 3..=10 } => {
            println!("valid id {id}");
        }
        Message::Hello { id } => {
            println!("other id {id}");
        }
    }
}

Binding the Whole Value

@ can also bind an entire structured value while a nested pattern checks its parts.

fn main() {
    let point = (3, 4);
    match point {
        p @ (x, _) if x > 0 => println!("{:?} has positive x", p),
        p => println!("{:?}", p),
    }
}

Combining with |

You can bind across alternative patterns. The name must appear in each alternative branch.

fn main() {
    let code = 404;
    match code {
        c @ (400 | 404 | 500) => println!("error code {c}"),
        c => println!("code {c}"),
    }
}

@ in if let

Bindings with @ work in if let too.

fn main() {
    let value = Some(50);
    if let Some(n @ 1..=100) = value {
        println!("in range: {n}");
    }
}

When to Reach for @

Use @ when you need both:

  • To restrict a value with a range or sub-pattern
  • To use that exact value in the arm body

Without both needs, a plain binding or pattern is simpler.

Readability

The form reads as name @ pattern: bind name if it matches pattern. Keep names descriptive so the intent is clear.

Bucketing with @

A common use is sorting a value into labeled buckets while keeping the original number for reporting.

fn classify(temp: i32) -> String {
    match temp {
        t @ ..=0 => format!("freezing ({t})"),
        t @ 1..=20 => format!("cool ({t})"),
        t @ 21..=30 => format!("warm ({t})"),
        t => format!("hot ({t})"),
    }
}

fn main() {
    println!("{}", classify(25));
}

Quick Check

What does the @ operator do in a pattern like id @ 1..=5?

Recap

You learned the @ binding operator:

  • It binds a value to a name while testing it against a pattern
  • Solves the lost-value problem with range patterns
  • Works with enum data, tuples, | alternatives, and if let
  • Use it when you need both the constraint and the value

Frequently asked questions

Is the “Binding with @” lesson free?

Yes — the full text of “Binding with @” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Binding with @”?

Capture and test. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Binding with @” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. match Deep Dive
  2. if let and while let
  3. Binding with @
  4. Destructuring
← Back to Learn Rust Coding