← Back to Rust Course | Chapter 8: Enums & Pattern Matching | Lesson 7 of 7

Destructuring क्या है

Destructuring एक value को एक साथ अलग-अलग named pieces में खोलना है, एक gift box खोलने और हर item निकालने जैसा।
Syntax
rust
let (a, b) = tuple;
let Name { field1, field2 } = value;
let [first, .., last] = array;

Destructuring a Tuple

एक let statement एक tuple को सीधे एक line में कई named variables में destructure कर सकता है, हर element को .0, .1, वगैरह से access करने के बजाय।

उदाहरण: Destructuring a Tuple

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `point`, set to `(3, 7)`
    let point = (3, 7);
    // Destructure `point` into `x, y`
    let (x, y) = point;
    // Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
    println!("x={}, y={}", x, y);
}

Destructuring a Struct

Struct fields को सीधे एक let statement में उसी नाम वाले local variables में destructure किया जा सकता है, struct के field names को pattern के रूप में उपयोग करते हुए।

उदाहरण: Destructuring a Struct

markup
// Define a struct `Point` to group related fields together
struct Point {
    x: i32,
    y: i32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p`, set to `Point { x: 5, y: 9 }`
    let p = Point { x: 5, y: 9 };
    let Point { x, y } = p;
    // Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
    println!("x={}, y={}", x, y);
}

Ignoring Fields with ..

जब आपको एक बड़े struct से सिर्फ कुछ fields चाहिए, pattern में .. Rust को बाकी बची सभी fields को individually नाम दिए बिना नज़रअंदाज़ करने को कहता है।

उदाहरण: Ignoring Fields with ..

markup
// Define a struct `Config` to group related fields together
struct Config {
    width: u32,
    height: u32,
    fullscreen: bool,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `cfg`, set to `Config { width: 1920, height: 1080, fullscreen: true }`
    let cfg = Config { width: 1920, height: 1080, fullscreen: true };
    let Config { width, .. } = cfg;
    // Print "Width: {}" to the console, substituting in `width` (with a trailing newline)
    println!("Width: {}", width);
}

Discarding Values with _

Underscore pattern _ किसी भी value को match करता है इसे discard करते हुए, destructuring या function parameters में उपयोगी जब एक value मौजूद होनी चाहिए लेकिन असल में ज़रूरत नहीं।

उदाहरण: Discarding Values with _

markup
// Entry point: execution of the program starts here
fn main() {
    // Destructure `(1, 2, 3)` into `first, _, third`
    let (first, _, third) = (1, 2, 3);
    // Print "first={}, third={}" to the console, substituting in `first, third` (with a trailing newline)
    println!("first={}, third={}", first, third);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी struct की सिर्फ कुछ fields को बाकी को explicitly नज़रअंदाज़ करने के लिए .. उपयोग किए बिना destructure करने की कोशिश करना, जो एक compile error है।
  2. यह भूल जाना कि _ pattern destructuring के दौरान किसी ज़रूरत न पड़ने वाली value को जानबूझकर discard करने के लिए उपयोग किया जा सकता है।
  3. यह मान लेना कि किसी reference को destructure करना अपने आप सब कुछ dereference कर देता है -- कभी-कभी एक explicit & pattern या .clone() चाहिए होता है।
चैप्टर सारांश
  • Destructuring एक single let या match में एक tuple, struct, या enum से कई named values निकालता है।
  • .. pattern उन बची हुई fields को नज़रअंदाज़ करने के लिए उपयोग किया जा सकता है जिनकी आपको परवाह नहीं।
  • _ pattern किसी भी single value को match करता और discard करता है इसे एक नाम से bind किए बिना।
  • Destructuring let statements, function parameters, और match arms में समान रूप से काम करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.