← Back to Rust Course | Chapter 11: Generics & Traits | Lesson 4 of 7

Trait Bounds क्या हैं

एक trait bound किसी generic function को बताता है "मुझे सिर्फ वे types दो जो पहले से यह specific चीज़ कर सकते हैं।"
Syntax
rust
fn name<T: Trait1 + Trait2>(param: T) {}
fn name(param: impl Trait) {}

Adding a Simple Trait Bound

Generic parameter list में एक colon के बाद एक trait bound generic type को सिर्फ उन types तक restrict करता है जो उस specific trait को implement करते हैं, function के अंदर इसके methods उपयोग करने की अनुमति देते हुए।

उदाहरण: Adding a Simple Trait Bound

markup
// Bring `std::fmt::Display` into scope
use std::fmt::Display;

fn print_it<T: Display>(item: T) {
    // Print "Item: {}" to the console, substituting in `item` (with a trailing newline)
    println!("Item: {}", item);
}

// Entry point: execution of the program starts here
fn main() {
    print_it(42);
    print_it("text");
}

Combining Multiple Trait Bounds

कई trait bounds को + से combine किया जा सकता है, generic type को इन सभी को एक साथ implement करने की ज़रूरत बनाते हुए।

उदाहरण: Combining Multiple Trait Bounds

markup
// Bring `std::fmt::Debug` into scope
use std::fmt::Debug;

fn show<T: Debug + Clone>(item: T) {
    // Declare an immutable variable `copy`, set to `item.clone()`
    let copy = item.clone();
    // Print "{:?}" to the console, substituting in `copy` (with a trailing newline)
    println!("{:?}", copy);
}

// Entry point: execution of the program starts here
fn main() {
    show(vec![1, 2, 3]);
}

impl Trait Parameter Shorthand

Parameter type के रूप में सीधे उपयोग किया गया impl TraitName एक simple generic bound के लिए shorthand है, तब उपयोगी जब आपको type parameter explicitly नाम देने की ज़रूरत न हो।

उदाहरण: impl Trait Parameter Shorthand

markup
// Bring `std::fmt::Display` into scope
use std::fmt::Display;

// Define the function `announce` taking `item`
fn announce(item: impl Display) {
    // Print "Announcing: {}" to the console, substituting in `item` (with a trailing newline)
    println!("Announcing: {}", item);
}

// Entry point: execution of the program starts here
fn main() {
    announce("New release");
}

Using a where Clause

कई generic parameters और bounds वाले functions के लिए, signature के बाद एक where clause function declaration को खुद ज़्यादा readable रखता है।

उदाहरण: Using a where Clause

markup
// Bring `std::fmt::Display` into scope
use std::fmt::Display;

fn combine<T, U>(a: T, b: U) -> String
where
    T: Display,
    U: Display,
{
    format!("{} and {}", a, b)
}

// Entry point: execution of the program starts here
fn main() {
    // Print "{}" to the console, substituting in `combine(1, "two")` (with a trailing newline)
    println!("{}", combine(1, "two"));
}
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. किसी trait की method को किसी generic function के अंदर call करने की कोशिश करना बिना पहले उस trait से type parameter constrain किए।
  2. अत्यधिक restrictive trait bounds लिखना जो उन types को exclude कर देते हैं जो अन्यथा ठीक काम करते।
  3. impl Trait parameter syntax को generic <T: Trait> syntax के साथ confuse करना -- ये closely related हैं लेकिन हमेशा interchangeable नहीं।
चैप्टर सारांश
  • एक trait bound, T: TraitName लिखा गया, एक generic type parameter को उस trait implement करने वाले types तक restrict करता है।
  • कई bounds को + से combine किया जा सकता है, जैसे T: Debug + Clone।
  • किसी parameter position में impl Trait एक simple, single-use trait bound के लिए shorthand syntax है।
  • where clauses bounds express करने के लिए एक alternative, ज़्यादा readable syntax प्रदान करते हैं, खासकर कई parameters के साथ।
🔒

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.