Generic Functions क्या हैं
In this page:
fn name<T>(param: T) -> T {
// body
}
name::<Type>(value);
Defining a Generic Function
एक generic type parameter, parameter list से पहले angle brackets में declared, एक single function definition को कई अलग-अलग concrete types में काम करने देता है।
उदाहरण: Defining a Generic Function
fn identity<T>(value: T) -> T {
value
}
// Entry point: execution of the program starts here
fn main() {
// Print "{}" to the console, substituting in `identity(5)` (with a trailing newline)
println!("{}", identity(5));
// Print "{}" to the console, substituting in `identity("hello")` (with a trailing newline)
println!("{}", identity("hello"));
}
Login to try C/C++/Java/PHP code in the editor
Generic Function with a Trait Bound
किसी generic function के अंदर comparison जैसे operations perform करने के लिए, आपको एक trait bound जोड़ना होगा जो compiler को बताए कि असल में कौन से types allowed हैं, जैसे T: PartialOrd।
उदाहरण: Generic Function with a Trait Bound
fn largest<T: PartialOrd>(a: T, b: T) -> T {
// Check whether `a > b { a } else { b }`
if a > b { a } else { b }
}
// Entry point: execution of the program starts here
fn main() {
// Print "{}" to the console, substituting in `largest(3, 7)` (with a trailing newline)
println!("{}", largest(3, 7));
// Print "{}" to the console, substituting in `largest(2.5, 1.1)` (with a trailing newline)
println!("{}", largest(2.5, 1.1));
}
Login to try C/C++/Java/PHP code in the editor
Generics Over Multiple Types
एक function में कई independent generic type parameters हो सकते हैं, हर एक potentially अलग trait bounds से constrained, इसे unrelated types को flexibly combine करने देते हुए।
उदाहरण: Generics Over Multiple Types
fn make_pair<A, B>(first: A, second: B) -> (A, B) {
(first, second)
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `pair`, set to `make_pair(1, "one")`
let pair = make_pair(1, "one");
// Print "{:?}" to the console, substituting in `pair` (with a trailing newline)
println!("{:?}", pair);
}
Login to try C/C++/Java/PHP code in the editor
Zero-Cost Abstraction
Rust compiler किसी generic function का एक specialized version हर concrete type के लिए generate करता है जिसके साथ यह उपयोग होता है (monomorphization कहलाता है), इसलिए एक generic function call करने में हाथ से अलग functions लिखने की तुलना में कोई runtime overhead नहीं है।
उदाहरण: Zero-Cost Abstraction
fn double<T: std::ops::Add<Output = T> + Copy>(value: T) -> T {
value + value
}
// Entry point: execution of the program starts here
fn main() {
// Print "{}" to the console, substituting in `double(21)` (with a trailing newline)
println!("{}", double(21));
// Print "{}" to the console, substituting in `double(1.5)` (with a trailing newline)
println!("{}", double(1.5));
}
Login to try C/C++/Java/PHP code in the editor
- Function signature में उपयोग करने से पहले generic type parameter (
<T>) declare करना भूल जाना। - यह मान लेना कि एक generic function
Tपर कोई भी operation perform कर सकता है -- trait bounds के बिना, सिर्फ वे operations allowed हैं जो हर possible type के लिए valid हैं। - हर concrete type के लिए एक अलग near-identical function लिखना बजाय एक single generic version निकालने के।
<T>लिखे गए Generic type parameters एक function को एक definition के साथ कई concrete types पर operate करने देते हैं।- Trait bounds के बिना, compiler सिर्फ उन operations को allow करता है जो बिल्कुल किसी भी type के लिए valid हैं।
T: PartialOrdजैसे Trait boundsTको उन types तक restrict करते हैं जो specific operations, जैसे comparison, support करते हैं।- Compiler उपयोग होने वाले हर concrete type के लिए specialized code generate करता है, इसलिए generics का कोई runtime performance cost नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: