Struct के Basics
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Creating Struct Instances
आप एक struct literal से एक struct instance बनाते हैं: type name के बाद curly braces जिसमें हर field के लिए एक value list है। सभी fields दी जानी चाहिए।
उदाहरण: Creating Struct Instances
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying Fields
Fields dot notation से पढ़े जाते हैं। अगर struct instance mut से bound है, इसके fields को creation के बाद individually भी reassign किया जा सकता है।
उदाहरण: Accessing and Modifying Fields
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Field Init Shorthand
जब किसी variable का नाम किसी struct field के नाम से मेल खाता है, आप field: field के बजाय सिर्फ variable लिखते हुए नाम दोहराना छोड़ सकते हैं।
उदाहरण: Field Init Shorthand
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- एक struct literal बनाते समय एक field भूल जाना, जिसे compiler reject करता है क्योंकि हर field ज़रूरी है।
- किसी struct instance की सिर्फ एक field को mutable बनाने की कोशिश करना -- mutability पूरी variable binding पर लागू होती है, per field नहीं।
- struct field access
instance.fieldको function calls के साथ confuse करना, यह भूल जाना कि plain field access के लिए कोई parentheses नहीं हैं।
- एक struct related named fields को
structkeyword से 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: