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

Tuple Structs क्या हैं

एक tuple struct values के एक group को एक नाम देता है बिना हर एक को individually नाम देने की परेशानी के।
Syntax
rust
struct Name(Type1, Type2);

let value = Name(v1, v2);
value.0

Defining a Tuple Struct

एक tuple struct एक tuple की simplicity को एक distinct, named type के साथ combine करता है। Fields में types हैं लेकिन कोई नाम नहीं, struct name के बाद parentheses में declared।

उदाहरण: Defining a Tuple Struct

markup
// Define a struct `Point` to group related fields together
struct Point(i32, i32);

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

Accessing Fields by Position

चूंकि tuple struct fields में कोई नाम नहीं, आप इन्हें इनके zero-based index के साथ dot notation उपयोग करके access करते हैं, बिल्कुल एक regular tuple की तरह।

उदाहरण: Accessing Fields by Position

markup
// Define a struct `Color` to group related fields together
struct Color(u8, u8, u8);

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `red`, set to `Color(255, 0, 0)`
    let red = Color(255, 0, 0);
    // Print "R:{} G:{} B:{}" to the console, substituting in `red.0, red.1, red.2` (with a trailing newline)
    println!("R:{} G:{} B:{}", red.0, red.1, red.2);
}

Distinct Types Even With Same Shape

उसी underlying type को wrap करने वाले दो tuple structs compiler के लिए अब भी पूरी तरह distinct types हैं, जो गलती से conceptually अलग values, जैसे meters और feet, को mix करने से रोकता है।

उदाहरण: Distinct Types Even With Same Shape

markup
// Define a struct `Meters` to group related fields together
struct Meters(f64);
// Define a struct `Feet` to group related fields together
struct Feet(f64);

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `distance`, set to `Meters(100.0)`
    let distance = Meters(100.0);
    // Print "Distance: {} meters" to the console, substituting in `distance.0` (with a trailing newline)
    println!("Distance: {} meters", distance.0);
}

Unit Structs

एक unit struct में बिल्कुल कोई fields नहीं होतीं और इसे सिर्फ एक नाम और एक semicolon से declare किया जाता है। इसे एक lightweight marker type के रूप में उपयोग किया जाता है, अक्सर कोई data store किए बिना एक trait implement करने के लिए।

उदाहरण: Unit Structs

markup
// Define a struct `Marker` to group related fields together
struct Marker;

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `_m`, set to `Marker`
    let _m = Marker;
    // Print "Unit struct created with zero fields" to the console (with a trailing newline)
    println!("Unit struct created with zero fields");
}
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. Tuple struct fields को नाम से access करने की कोशिश करना -- वे सिर्फ position से accessible हैं, जैसे .0 और .1।
  2. एक जैसी underlying field types वाले दो अलग tuple structs को confuse करना, जैसे Meters(f64) और Feet(f64), जिन्हें Rust पूरी तरह distinct types मानता है।
  3. यह भूल जाना कि एक unit struct (बिल्कुल कोई fields नहीं) भी valid syntax है, जो विशुद्ध रूप से एक marker type के रूप में उपयोग होता है।
चैप्टर सारांश
  • एक tuple struct struct Name(Type1, Type2); जैसे declared होता है, unnamed, positional fields के साथ।
  • किसी tuple struct की fields .0, .1, वगैरह उपयोग करके access की जाती हैं।
  • भले ही दो tuple structs उसी underlying type को wrap करें, वे distinct, incompatible types हैं।
  • एक unit struct (struct Marker;) में बिल्कुल कोई fields नहीं होतीं और इसे विशुद्ध रूप से एक type-level marker के रूप में उपयोग किया जाता है।
🔒

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.