← Back to Rust Course | Chapter 4: Functions | Lesson 4 of 6

Closures के Basics

एक closure एक छोटा, portable function है जिसे आप तुरंत बना सकते हैं जो यह याद रखता है कि यह कहां लिखा गया था उसके आस-पास क्या था।
Syntax
rust
let name = |param| expression;
let name = |param: Type| -> ReturnType {
    // body
};

Defining a Closure

एक closure अपनी parameter list के आस-पास pipes | | उपयोग करके लिखा जाता है, उसके बाद एक expression या block। Closures को एक variable में store किया जा सकता है और फिर बिल्कुल एक function की तरह call किया जा सकता है।

उदाहरण: Defining a Closure

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `add_one`, set to `|x: i32| x + 1`
    let add_one = |x: i32| x + 1;
    // Print "{}" to the console, substituting in `add_one(5)` (with a trailing newline)
    println!("{}", add_one(5));
}

Capturing Variables from the Environment

Regular functions के विपरीत, closures उस scope की variables सीधे उपयोग कर सकते हैं जहां उन्हें define किया गया था, उन्हें parameters के रूप में receive करने की ज़रूरत के बिना।

उदाहरण: Capturing Variables from the Environment

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `multiplier`, set to `3`
    let multiplier = 3;
    // Declare an immutable variable `multiply`, set to `|x: i32| x * multiplier`
    let multiply = |x: i32| x * multiplier;
    // Print "{}" to the console, substituting in `multiply(7)` (with a trailing newline)
    println!("{}", multiply(7));
}

Type Inference in Closures

Closures को आमतौर पर explicit parameter या return types annotated करने की ज़रूरत नहीं -- Rust इन्हें इस आधार पर infer करता है कि closure कैसे उपयोग होता है, closure syntax को compact रखते हुए।

उदाहरण: Type Inference in Closures

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `square`, set to `|x| x * x`
    let square = |x| x * x;
    // Declare an immutable variable `result` (an integer), set to `square(6)`
    let result: i32 = square(6);
    // Print "{}" to the console, substituting in `result` (with a trailing newline)
    println!("{}", result);
}

Multi-line Closure Bodies

जब एक closure को एक से ज़्यादा expression चाहिए, इसकी body curly braces में एक पूरा block हो सकती है, बिल्कुल एक function body की तरह।

उदाहरण: Multi-line Closure Bodies

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `describe`, set to `|n: i32| {`
    let describe = |n: i32| {
        // Declare an immutable variable `kind`, set to `if n % 2 == 0 { "even" } else { "odd" }`
        let kind = if n % 2 == 0 { "even" } else { "odd" };
        format!("{} is {}", n, kind)
    };
    // Print "{}" to the console, substituting in `describe(9)` (with a trailing newline)
    println!("{}", describe(9));
}
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. यह भूल जाना कि closures अपने आप अपने आस-पास के scope से variables capture कर सकते हैं, regular fn functions के विपरीत।
  2. हर closure parameter पर अनावश्यक type annotations लिखना, जब Rust आमतौर पर इन्हें usage से infer कर सकता है।
  3. अनजान code पढ़ते समय closure syntax |x| x + 1 को एक bitwise OR expression के साथ confuse करना।
चैप्टर सारांश
  • Closures anonymous functions हैं जो parameters के आस-पास pipes से लिखे जाते हैं, जैसे |x| x + 1।
  • Closures उस environment से variables capture कर सकते हैं जिसमें उन्हें define किया गया था।
  • Closure parameter और return types आमतौर पर infer किए जाते हैं, regular function definitions के विपरीत।
  • Closures को variables में store किया जा सकता है और values की तरह pass किया जा सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.