Trait Bounds क्या हैं
In this page:
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
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Combining Multiple Trait Bounds
कई trait bounds को + से combine किया जा सकता है, generic type को इन सभी को एक साथ implement करने की ज़रूरत बनाते हुए।
उदाहरण: Combining Multiple Trait Bounds
// 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]);
}
Login to try C/C++/Java/PHP code in the editor
impl Trait Parameter Shorthand
Parameter type के रूप में सीधे उपयोग किया गया impl TraitName एक simple generic bound के लिए shorthand है, तब उपयोगी जब आपको type parameter explicitly नाम देने की ज़रूरत न हो।
उदाहरण: impl Trait Parameter Shorthand
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Using a where Clause
कई generic parameters और bounds वाले functions के लिए, signature के बाद एक where clause function declaration को खुद ज़्यादा readable रखता है।
उदाहरण: Using a where Clause
// 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"));
}
Login to try C/C++/Java/PHP code in the editor
- किसी trait की method को किसी generic function के अंदर call करने की कोशिश करना बिना पहले उस trait से type parameter constrain किए।
- अत्यधिक restrictive trait bounds लिखना जो उन types को exclude कर देते हैं जो अन्यथा ठीक काम करते।
impl Traitparameter 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 है। whereclauses bounds express करने के लिए एक alternative, ज़्यादा readable syntax प्रदान करते हैं, खासकर कई parameters के साथ।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: