← Back to Rust Course | Chapter 7: Structs | Lesson 6 of 7

Struct का Update Syntax

Struct update syntax आपको एक नया struct बनाने देता है जो ज़्यादातर एक पुराने की copy है, लेकिन कुछ fields swapped के साथ।
Syntax
rust
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

markup
// 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);
}

Moving Non-Copy Fields

अगर base struct में String जैसी non-Copy fields हैं जो override नहीं की गईं, वे fields नए instance में move हो जाती हैं, original struct को बाद में partially unusable बनाते हुए।

उदाहरण: Moving Non-Copy Fields

markup
// 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);
}

..base Must Come Last

..base syntax को हमेशा struct literal में आखिरी item के रूप में आना चाहिए, हर explicitly assigned field के बाद, क्योंकि यह जो कुछ बचा है उसे भरता है।

उदाहरण: ..base Must Come Last

markup
// 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);
}

Combining With Copy Structs

जब base struct की सभी fields Copy types हों, struct update syntax उपयोग करना original instance को बाद में पूरी तरह usable छोड़ता है, क्योंकि इससे कुछ भी move नहीं हुआ।

उदाहरण: Combining With Copy Structs

markup
// 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);
}
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 update syntax ..other, other से non-Copy fields move करता है, संभवतः इसे बाद में invalidate करते हुए।
  2. ..other को दूसरी field assignments से पहले रखना -- इसे struct literal में आखिरी आना चाहिए।
  3. यह मान लेना कि 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:

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.