Destructuring क्या है
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Destructuring a Struct
Struct fields को सीधे एक let statement में उसी नाम वाले local variables में destructure किया जा सकता है, struct के field names को pattern के रूप में उपयोग करते हुए।
उदाहरण: Destructuring a Struct
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Ignoring Fields with ..
जब आपको एक बड़े struct से सिर्फ कुछ fields चाहिए, pattern में .. Rust को बाकी बची सभी fields को individually नाम दिए बिना नज़रअंदाज़ करने को कहता है।
उदाहरण: Ignoring Fields with ..
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Discarding Values with _
Underscore pattern _ किसी भी value को match करता है इसे discard करते हुए, destructuring या function parameters में उपयोगी जब एक value मौजूद होनी चाहिए लेकिन असल में ज़रूरत नहीं।
उदाहरण: Discarding Values with _
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी struct की सिर्फ कुछ fields को बाकी को explicitly नज़रअंदाज़ करने के लिए
..उपयोग किए बिना destructure करने की कोशिश करना, जो एक compile error है। - यह भूल जाना कि
_pattern destructuring के दौरान किसी ज़रूरत न पड़ने वाली value को जानबूझकर discard करने के लिए उपयोग किया जा सकता है। - यह मान लेना कि किसी reference को destructure करना अपने आप सब कुछ dereference कर देता है -- कभी-कभी एक explicit
&pattern या.clone()चाहिए होता है।
- Destructuring एक single
letयाmatchमें एक tuple, struct, या enum से कई named values निकालता है। ..pattern उन बची हुई fields को नज़रअंदाज़ करने के लिए उपयोग किया जा सकता है जिनकी आपको परवाह नहीं।_pattern किसी भी single value को match करता और discard करता है इसे एक नाम से bind किए बिना।- Destructuring
letstatements, function parameters, और match arms में समान रूप से काम करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: