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

Where Clauses क्या हैं

एक where clause एक तरफ, एक tidy list है, उन सभी rules की जो किसी function के types को follow करनी हैं।
Syntax
rust
fn name<T, U>(a: T, b: U) -> ReturnType
where
    T: Trait1 + Trait2,
    U: Trait3,
{
    // body
}

Basic where Clause

एक where clause function signature के बाद trait bounds को अलग से list करता है, parameter list को खुद clean और पढ़ने में आसान रखते हुए।

उदाहरण: Basic where Clause

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

fn describe<T>(item: T) -> String
where
    T: Display,
{
    format!("Item: {}", item)
}

// Entry point: execution of the program starts here
fn main() {
    // Print "{}" to the console, substituting in `describe(99)` (with a trailing newline)
    println!("{}", describe(99));
}

Multiple Bounds in a where Clause

जब किसी function में अपने bounds चाहिए वाले कई generic parameters हों, एक where clause इन सभी को साफ़ रूप से list करता है, एक cramped inline list के बजाय, एक per line।

उदाहरण: Multiple Bounds in a where Clause

markup
// Bring `std::fmt::{Debug, Display}` into scope
use std::fmt::{Debug, Display};

fn compare<T, U>(a: T, b: U) -> String
where
    T: Display,
    U: Debug,
{
    format!("{} vs {:?}", a, b)
}

// Entry point: execution of the program starts here
fn main() {
    // Print "{}" to the console, substituting in `compare(5, vec![1, 2])` (with a trailing newline)
    println!("{}", compare(5, vec![1, 2]));
}

Bounding a Generic Container Type

where clauses ज़्यादा complex generic types पर bounds express कर सकते हैं, जैसे किसी Vec<T> के अंदर elements को एक particular trait implement करने की ज़रूरत।

उदाहरण: Bounding a Generic Container Type

markup
fn print_all<T>(items: Vec<T>)
where
    T: std::fmt::Display,
{
    // Loop over `items`, binding each item to `item`
    for item in items {
        // Print "{}" to the console, substituting in `item` (with a trailing newline)
        println!("{}", item);
    }
}

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

where Clauses vs Inline Bounds

Inline bounds और where clauses बिल्कुल वही constraints express करते हैं -- choice विशुद्ध रूप से readability के बारे में है, where clauses आमतौर पर तब पसंद किए जाते हैं जब bounds कई या complex हो जाते हैं।

उदाहरण: where Clauses vs Inline Bounds

markup
fn largest<T: PartialOrd + Copy>(items: &[T]) -> T
where
    T: std::fmt::Debug,
{
    // Declare a mutable variable `largest`, initialized to `items[0]`
    let mut largest = items[0];
    // Loop over `items`, binding each item to `&item`
    for &item in items {
        // Check whether `item > largest`
        if item > largest {
            largest = item;
        }
    }
    largest
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `numbers`, set to `[3, 7, 2, 9, 4]`
    let numbers = [3, 7, 2, 9, 4];
    // Print "{:?}" to the console, substituting in `largest(&numbers)` (with a trailing newline)
    println!("{:?}", largest(&numbers));
}
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. where clause उपयोग करने के बजाय सीधे generic parameter list में कई trait bounds ठूंसना, function signature को पढ़ना मुश्किल बनाते हुए।
  2. यह मान लेना कि where clauses कुछ express कर सकते हैं जो inline bounds नहीं कर सकते -- वे power में समान हैं, सिर्फ complex cases के लिए ज़्यादा readable।
  3. यह भूल जाना कि एक where clause को अब भी हर bound का ज़रूरत है कि function call होने पर उपयोग किए असली types से satisfiable हो।
चैप्टर सारांश
  • where clauses trait bounds को angle brackets से बाहर function signature के बाद एक dedicated section में move करते हैं।
  • ये functionally inline bounds के बराबर हैं, लेकिन कई constrained parameters वाले functions के लिए readability काफी बेहतर करते हैं।
  • where clauses complex types पर bounds express कर सकते हैं, जैसे T: Clone की ज़रूरत वाला Vec<T>।
  • Idiomatic Rust where clauses पसंद करता है एक बार किसी function में एक या दो से ज़्यादा simple bounds हों।
🔒

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.