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

Struct के Basics

एक struct एक custom box है जिसे आप खुद design करते हैं, related data के अलग-अलग टुकड़ों को एक साथ रखने के लिए named compartments के साथ।
Syntax
rust
struct Name {
    field1: Type1,
    field2: Type2,
}

let value = Name { field1: v1, field2: v2 };
value.field1

Defining a Struct

struct keyword named fields के साथ एक नया custom type define करता है, हर एक अपने type के साथ। यह अलग loose variables उपयोग करने के बजाय related data को एक meaningful नाम के तहत group करता है।

उदाहरण: Defining a Struct

markup
// Define a struct `User` to group related fields together
struct User {
    name: String,
    age: u32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `user`, set to `User { name: String::from("Ada"), age: 30 }`
    let user = User { name: String::from("Ada"), age: 30 };
    // Print "{} is {} years old" to the console, substituting in `user.name, user.age` (with a trailing newline)
    println!("{} is {} years old", user.name, user.age);
}

Creating Struct Instances

आप एक struct literal से एक struct instance बनाते हैं: type name के बाद curly braces जिसमें हर field के लिए एक value list है। सभी fields दी जानी चाहिए।

उदाहरण: Creating Struct Instances

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

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

Accessing and Modifying Fields

Fields dot notation से पढ़े जाते हैं। अगर struct instance mut से bound है, इसके fields को creation के बाद individually भी reassign किया जा सकता है।

उदाहरण: Accessing and Modifying Fields

markup
// Define a struct `Counter` to group related fields together
struct Counter {
    value: i32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `counter`, initialized to `Counter { value: 0 }`
    let mut counter = Counter { value: 0 };
    counter.value += 1;
    counter.value += 1;
    // Print "Counter is at {}" to the console, substituting in `counter.value` (with a trailing newline)
    println!("Counter is at {}", counter.value);
}

Field Init Shorthand

जब किसी variable का नाम किसी struct field के नाम से मेल खाता है, आप field: field के बजाय सिर्फ variable लिखते हुए नाम दोहराना छोड़ सकते हैं।

उदाहरण: Field Init Shorthand

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

// Define the function `make_point` taking `x`, `y`, `z` and returning a `Point3D`
fn make_point(x: i32, y: i32, z: i32) -> Point3D {
    Point3D { x, y, z }
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p`, set to `make_point(1, 2, 3)`
    let p = make_point(1, 2, 3);
    // Print "({}, {}, {})" to the console, substituting in `p.x, p.y, p.z` (with a trailing newline)
    println!("({}, {}, {})", p.x, p.y, p.z);
}
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 literal बनाते समय एक field भूल जाना, जिसे compiler reject करता है क्योंकि हर field ज़रूरी है।
  2. किसी struct instance की सिर्फ एक field को mutable बनाने की कोशिश करना -- mutability पूरी variable binding पर लागू होती है, per field नहीं।
  3. struct field access instance.field को function calls के साथ confuse करना, यह भूल जाना कि plain field access के लिए कोई parentheses नहीं हैं।
चैप्टर सारांश
  • एक struct related named fields को struct keyword से defined एक custom type के तहत group करता है।
  • Struct instances एक struct literal से बनाए जाते हैं जो हर field की value list करता है।
  • Fields dot notation से access होते हैं, जैसे instance.field।
  • किसी instance variable को mut चिह्नित करना बाद में इसकी किसी भी field को बदलने देता है।
🔒

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.