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

impl Blocks का उपयोग

एक impl block वह जगह है जहां आप एक struct को functions लिखकर चीज़ें करना सिखाते हैं जो इससे belong करते हैं।
Syntax
rust
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

markup
// 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());
}

Multiple Methods in One impl Block

एक single impl block ज़रूरत के अनुसार कई methods define कर सकता है, हर एक उस struct के किसी भी instance पर dot notation उपयोग करके callable बनते हुए।

उदाहरण: Multiple Methods in One impl Block

markup
// 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());
}

Multiple impl Blocks for the Same Struct

Rust किसी struct के behavior को कई impl blocks में split करने देता है; इन्हें compiler द्वारा merge कर दिया जाता है, जो बड़ी मात्रा में functionality organize करने के लिए उपयोगी हो सकता है।

उदाहरण: Multiple impl Blocks for the Same Struct

markup
// 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());
}

Methods Can Take Extra Parameters

self से आगे, methods regular functions की तरह ही additional parameters accept कर सकते हैं, उन्हें struct के अपने data के साथ-साथ बाहरी input के आधार पर results compute करने देते हुए।

उदाहरण: Methods Can Take Extra Parameters

markup
// 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));
}
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. Free-standing functions लिखना जो किसी struct को अपने पहले argument के रूप में लेते हैं बजाय उन्हें एक impl block में group करने के।
  2. यह भूल जाना कि एक struct के कई impl blocks हो सकते हैं -- Rust organization के लिए इन्हें split करने देता है।
  3. उसी impl block के अंदर लिखते समय methods (जो self लेते हैं) को associated functions (जो नहीं लेते) के साथ confuse करना।
चैप्टर सारांश
  • impl TypeName { ... } functions को किसी particular struct type के साथ associate करता है।
  • किसी impl block के अंदर functions जो अपने पहले parameter के रूप में self लेते हैं उन्हें methods कहते हैं।
  • एक struct में एक से ज़्यादा impl block हो सकते हैं; Rust इन्हें एक साथ merge करता है।
  • impl blocks में behavior group करना related logic को उस data के पास organized रखता है जिस पर यह operate करता है।
🔒

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.