Yapılar (Structs)

Demetler (tuples) gibi, yapılar (structs) da eşleştirme (matching) yoluyla çözümlenebilir (destructured):

struct Foo {
    x: (u32, u32),
    y: u32,
}

#[rustfmt::skip]
fn main() {
    let foo = Foo { x: (1, 2), y: 3 };
    match foo {
        Foo { y: 2, x: i }   => println!("y = 2, x = {i:?}"),
        Foo { x: (1, b), y } => println!("x.0 = 1, b = {b}, y = {y}"),
        Foo { y, .. }        => println!("y = {y}, diğer alanlar yoksayıldı"),
    }
}
This slide should take about 4 minutes.
  • foo’daki değişmez (literal) değerleri diğer desenlerle eşleşecek şekilde değiştirin.
  • Foo’ya yeni bir alan ekleyin ve desende gerektiği gibi değişiklikler yapın.

Daha Fazlasını Keşfedin

  • match &foo’yu deneyin ve yakalanan (captures) değerlerin türünü kontrol edin. Desen sözdizimi aynı kalır, ancak yakalananlar paylaşılan referanslar (shared references) haline gelir. Bu eşleşme ergonomisidir (match ergonomics) ve bir enum üzerinde metotlar uygularken match self ile genellikle kullanışlıdır.
    • Aynı etki match &mut foo ile de meydana gelir: yakalanan (captures) değerler özel (exclusive) referanslar haline gelir.
  • The distinction between a capture and a constant expression can be hard to spot. Try changing the 2 in the first arm to a variable, and see that it subtly doesn’t work. Change it to a const and see it working again.