Tuple Structs क्या हैं
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Accessing Fields by Position
चूंकि tuple struct fields में कोई नाम नहीं, आप इन्हें इनके zero-based index के साथ dot notation उपयोग करके access करते हैं, बिल्कुल एक regular tuple की तरह।
उदाहरण: Accessing Fields by Position
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Unit Structs
एक unit struct में बिल्कुल कोई fields नहीं होतीं और इसे सिर्फ एक नाम और एक semicolon से declare किया जाता है। इसे एक lightweight marker type के रूप में उपयोग किया जाता है, अक्सर कोई data store किए बिना एक trait implement करने के लिए।
उदाहरण: Unit Structs
// 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");
}
Login to try C/C++/Java/PHP code in the editor
- Tuple struct fields को नाम से access करने की कोशिश करना -- वे सिर्फ position से accessible हैं, जैसे
.0और.1। - एक जैसी underlying field types वाले दो अलग tuple structs को confuse करना, जैसे
Meters(f64)औरFeet(f64), जिन्हें Rust पूरी तरह distinct types मानता है। - यह भूल जाना कि एक 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: