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

Generic Functions क्या हैं

एक generic function एक बार लिखा जाता है लेकिन कई अलग-अलग types की values के साथ काम कर सकता है, एक universal tool की तरह।
Syntax
rust
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

markup
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"));
}

Generic Function with a Trait Bound

किसी generic function के अंदर comparison जैसे operations perform करने के लिए, आपको एक trait bound जोड़ना होगा जो compiler को बताए कि असल में कौन से types allowed हैं, जैसे T: PartialOrd।

उदाहरण: Generic Function with a Trait Bound

markup
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));
}

Generics Over Multiple Types

एक function में कई independent generic type parameters हो सकते हैं, हर एक potentially अलग trait bounds से constrained, इसे unrelated types को flexibly combine करने देते हुए।

उदाहरण: Generics Over Multiple Types

markup
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);
}

Zero-Cost Abstraction

Rust compiler किसी generic function का एक specialized version हर concrete type के लिए generate करता है जिसके साथ यह उपयोग होता है (monomorphization कहलाता है), इसलिए एक generic function call करने में हाथ से अलग functions लिखने की तुलना में कोई runtime overhead नहीं है।

उदाहरण: Zero-Cost Abstraction

markup
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));
}
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. Function signature में उपयोग करने से पहले generic type parameter (<T>) declare करना भूल जाना।
  2. यह मान लेना कि एक generic function T पर कोई भी operation perform कर सकता है -- trait bounds के बिना, सिर्फ वे operations allowed हैं जो हर possible type के लिए valid हैं।
  3. हर 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 bounds T को उन 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:

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.