Struct का Update Syntax
In this page:
let new_value = Name {
field1: v1,
..old_value
};
Basic Struct Update Syntax
Struct update syntax आपको एक मौजूदा instance के आधार पर एक नया instance बनाने देता है, explicitly सिर्फ उन fields को override करते हुए जो आप बदलना चाहते हैं और बाकी को ..base से copy करते हुए।
उदाहरण: Basic Struct Update Syntax
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// 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 `default_config`, set to `Config { width: 800, height: 600, fullscreen: false }`
let default_config = Config { width: 800, height: 600, fullscreen: false };
// Declare an immutable variable `custom`, set to `Config { fullscreen: true, ..default_config }`
let custom = Config { fullscreen: true, ..default_config };
// Print "{:?}" to the console, substituting in `custom` (with a trailing newline)
println!("{:?}", custom);
}
Login to try C/C++/Java/PHP code in the editor
Moving Non-Copy Fields
अगर base struct में String जैसी non-Copy fields हैं जो override नहीं की गईं, वे fields नए instance में move हो जाती हैं, original struct को बाद में partially unusable बनाते हुए।
उदाहरण: Moving Non-Copy Fields
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Profile` to group related fields together
struct Profile {
username: String,
active: bool,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `base`, set to `Profile { username: String::from("guest"), active: false }`
let base = Profile { username: String::from("guest"), active: false };
// Declare an immutable variable `updated`, set to `Profile { active: true, ..base }`
let updated = Profile { active: true, ..base };
// Print "{:?}" to the console, substituting in `updated` (with a trailing newline)
println!("{:?}", updated);
}
Login to try C/C++/Java/PHP code in the editor
..base Must Come Last
..base syntax को हमेशा struct literal में आखिरी item के रूप में आना चाहिए, हर explicitly assigned field के बाद, क्योंकि यह जो कुछ बचा है उसे भरता है।
उदाहरण: ..base Must Come Last
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Point3D` to group related fields together
struct Point3D {
x: i32,
y: i32,
z: i32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `origin`, set to `Point3D { x: 0, y: 0, z: 0 }`
let origin = Point3D { x: 0, y: 0, z: 0 };
// Declare an immutable variable `shifted`, set to `Point3D { x: 5, ..origin }`
let shifted = Point3D { x: 5, ..origin };
// Print "{:?}" to the console, substituting in `shifted` (with a trailing newline)
println!("{:?}", shifted);
}
Login to try C/C++/Java/PHP code in the editor
Combining With Copy Structs
जब base struct की सभी fields Copy types हों, struct update syntax उपयोग करना original instance को बाद में पूरी तरह usable छोड़ता है, क्योंकि इससे कुछ भी move नहीं हुआ।
उदाहरण: Combining With Copy Structs
// Auto-generate #[derive(Debug, Clone, Copy)] for the type below
#[derive(Debug, Clone, Copy)]
// Define a struct `Settings` to group related fields together
struct Settings {
volume: u8,
brightness: u8,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `defaults`, set to `Settings { volume: 50, brightness: 70 }`
let defaults = Settings { volume: 50, brightness: 70 };
// Declare an immutable variable `louder`, set to `Settings { volume: 80, ..defaults }`
let louder = Settings { volume: 80, ..defaults };
// Print "defaults still usable: {:?}" to the console, substituting in `defaults` (with a trailing newline)
println!("defaults still usable: {:?}", defaults);
// Print "louder: {:?}" to the console, substituting in `louder` (with a trailing newline)
println!("louder: {:?}", louder);
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि struct update syntax
..other,otherसे non-Copyfields move करता है, संभवतः इसे बाद में invalidate करते हुए। ..otherको दूसरी field assignments से पहले रखना -- इसे struct literal में आखिरी आना चाहिए।- यह मान लेना कि struct update syntax original struct को जगह पर mutate करता है, जब यह असल में एक बिल्कुल नया instance बनाता है।
- किसी struct literal के आखिर में
..other_instanceएक मौजूदा instance से बची हुई किसी भी fields को भरता है। - सिर्फ वे fields जो explicitly listed नहीं हैं base instance से copy या move होती हैं।
- अगर base instance से कोई moved (non-Copy) fields ली गई हों, वह instance बाद में उपयोग नहीं किया जा सकता।
- यह syntax हर field दोहराए बिना किसी struct की variations बनाने का एक concise तरीका है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: