0Pricing
Learn Rust Coding · Ders

@ ile Bağlama

Yakalayın ve test edin

@ ile Bağlama, CoddyKit'te ücretsiz bir Learn Rust Coding dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Learn Rust Coding öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Learn Rust Coding kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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

Sıkça Sorulan Sorular

“@ ile Bağlama” dersi ücretsiz mi?

Evet — “@ ile Bağlama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Learn Rust Coding kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Learn Rust Coding kursu toplamda 4 dersten oluşur.

“@ ile Bağlama” dersinde ne öğreneceğim?

Yakalayın ve test edin Learn Rust Coding ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Learn Rust Coding öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Learn Rust Coding, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“@ ile Bağlama” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Learn Rust Coding dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Learn Rust Coding dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. match Derinlemesine İnceleme
  2. if let ve while let
  3. @ ile Bağlama
  4. Yapı Bozma
← Learn Rust Coding Sayfasına Dön