Where Clauses क्या हैं
where clause एक तरफ, एक tidy list है, उन सभी rules की जो किसी function के types को follow करनी हैं।In this page:
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
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Multiple Bounds in a where Clause
जब किसी function में अपने bounds चाहिए वाले कई generic parameters हों, एक where clause इन सभी को साफ़ रूप से list करता है, एक cramped inline list के बजाय, एक per line।
उदाहरण: Multiple Bounds in a where Clause
// 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]));
}
Login to try C/C++/Java/PHP code in the editor
Bounding a Generic Container Type
where clauses ज़्यादा complex generic types पर bounds express कर सकते हैं, जैसे किसी Vec<T> के अंदर elements को एक particular trait implement करने की ज़रूरत।
उदाहरण: Bounding a Generic Container Type
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]);
}
Login to try C/C++/Java/PHP code in the editor
where Clauses vs Inline Bounds
Inline bounds और where clauses बिल्कुल वही constraints express करते हैं -- choice विशुद्ध रूप से readability के बारे में है, where clauses आमतौर पर तब पसंद किए जाते हैं जब bounds कई या complex हो जाते हैं।
उदाहरण: where Clauses vs Inline Bounds
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));
}
Login to try C/C++/Java/PHP code in the editor
whereclause उपयोग करने के बजाय सीधे generic parameter list में कई trait bounds ठूंसना, function signature को पढ़ना मुश्किल बनाते हुए।- यह मान लेना कि
whereclauses कुछ express कर सकते हैं जो inline bounds नहीं कर सकते -- वे power में समान हैं, सिर्फ complex cases के लिए ज़्यादा readable। - यह भूल जाना कि एक
whereclause को अब भी हर bound का ज़रूरत है कि function call होने पर उपयोग किए असली types से satisfiable हो।
whereclauses trait bounds को angle brackets से बाहर function signature के बाद एक dedicated section में move करते हैं।- ये functionally inline bounds के बराबर हैं, लेकिन कई constrained parameters वाले functions के लिए readability काफी बेहतर करते हैं।
whereclauses complex types पर bounds express कर सकते हैं, जैसेT: Cloneकी ज़रूरत वालाVec<T>।- Idiomatic Rust
whereclauses पसंद करता है एक बार किसी function में एक या दो से ज़्यादा simple bounds हों।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: