impl Blocks का उपयोग
impl block वह जगह है जहां आप एक struct को functions लिखकर चीज़ें करना सिखाते हैं जो इससे belong करते हैं।In this page:
impl Name {
fn method(&self) -> ReturnType {
// body
}
}
Defining an impl Block
एक impl block functions को एक specific struct के साथ associate करता है, आपको unrelated free functions बिखेरने के बजाय behavior को उस data के साथ group करने देते हुए जिस पर यह operate करता है।
उदाहरण: Defining an impl Block
// Define a struct `Rectangle` to group related fields together
struct Rectangle {
width: f64,
height: f64,
}
// Implement methods on `Rectangle`
impl Rectangle {
// Define the function `area` taking `&self` and returning a floating-point number
fn area(&self) -> f64 {
self.width * self.height
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `rect`, set to `Rectangle { width: 4.0, height: 3.0 }`
let rect = Rectangle { width: 4.0, height: 3.0 };
// Print "Area: {}" to the console, substituting in `rect.area()` (with a trailing newline)
println!("Area: {}", rect.area());
}
Login to try C/C++/Java/PHP code in the editor
Multiple Methods in One impl Block
एक single impl block ज़रूरत के अनुसार कई methods define कर सकता है, हर एक उस struct के किसी भी instance पर dot notation उपयोग करके callable बनते हुए।
उदाहरण: Multiple Methods in One impl Block
// Define a struct `Rectangle` to group related fields together
struct Rectangle {
width: f64,
height: f64,
}
// Implement methods on `Rectangle`
impl Rectangle {
// Define the function `area` taking `&self` and returning a floating-point number
fn area(&self) -> f64 {
self.width * self.height
}
// Define the function `perimeter` taking `&self` and returning a floating-point number
fn perimeter(&self) -> f64 {
2.0 * (self.width + self.height)
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `rect`, set to `Rectangle { width: 5.0, height: 2.0 }`
let rect = Rectangle { width: 5.0, height: 2.0 };
// Print "Area: {}, Perimeter: {}" to the console, substituting in `rect.area(), rect.perimeter()` (with a trailing newline)
println!("Area: {}, Perimeter: {}", rect.area(), rect.perimeter());
}
Login to try C/C++/Java/PHP code in the editor
Multiple impl Blocks for the Same Struct
Rust किसी struct के behavior को कई impl blocks में split करने देता है; इन्हें compiler द्वारा merge कर दिया जाता है, जो बड़ी मात्रा में functionality organize करने के लिए उपयोगी हो सकता है।
उदाहरण: Multiple impl Blocks for the Same Struct
// Define a struct `Circle` to group related fields together
struct Circle {
radius: f64,
}
// Implement methods on `Circle`
impl Circle {
// Define the function `area` taking `&self` and returning a floating-point number
fn area(&self) -> f64 {
3.14159 * self.radius * self.radius
}
}
// Implement methods on `Circle`
impl Circle {
// Define the function `diameter` taking `&self` and returning a floating-point number
fn diameter(&self) -> f64 {
self.radius * 2.0
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `c`, set to `Circle { radius: 3.0 }`
let c = Circle { radius: 3.0 };
// Print "Area: {:.2}, Diameter: {}" to the console, substituting in `c.area(), c.diameter()` (with a trailing newline)
println!("Area: {:.2}, Diameter: {}", c.area(), c.diameter());
}
Login to try C/C++/Java/PHP code in the editor
Methods Can Take Extra Parameters
self से आगे, methods regular functions की तरह ही additional parameters accept कर सकते हैं, उन्हें struct के अपने data के साथ-साथ बाहरी input के आधार पर results compute करने देते हुए।
उदाहरण: Methods Can Take Extra Parameters
// Define a struct `Rectangle` to group related fields together
struct Rectangle {
width: f64,
height: f64,
}
// Implement methods on `Rectangle`
impl Rectangle {
// Define the function `scaled_area` taking `&self`, `factor` and returning a floating-point number
fn scaled_area(&self, factor: f64) -> f64 {
self.width * self.height * factor
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `rect`, set to `Rectangle { width: 2.0, height: 3.0 }`
let rect = Rectangle { width: 2.0, height: 3.0 };
// Print "Scaled area: {}" to the console, substituting in `rect.scaled_area(2.0)` (with a trailing newline)
println!("Scaled area: {}", rect.scaled_area(2.0));
}
Login to try C/C++/Java/PHP code in the editor
- Free-standing functions लिखना जो किसी struct को अपने पहले argument के रूप में लेते हैं बजाय उन्हें एक
implblock में group करने के। - यह भूल जाना कि एक struct के कई
implblocks हो सकते हैं -- Rust organization के लिए इन्हें split करने देता है। - उसी
implblock के अंदर लिखते समय methods (जोselfलेते हैं) को associated functions (जो नहीं लेते) के साथ confuse करना।
impl TypeName { ... }functions को किसी particular struct type के साथ associate करता है।- किसी
implblock के अंदर functions जो अपने पहले parameter के रूप मेंselfलेते हैं उन्हें methods कहते हैं। - एक struct में एक से ज़्यादा
implblock हो सकते हैं; Rust इन्हें एक साथ merge करता है। implblocks में behavior group करना related logic को उस data के पास organized रखता है जिस पर यह operate करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: