Closures के Basics
In this page:
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
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Capturing Variables from the Environment
Regular functions के विपरीत, closures उस scope की variables सीधे उपयोग कर सकते हैं जहां उन्हें define किया गया था, उन्हें parameters के रूप में receive करने की ज़रूरत के बिना।
उदाहरण: Capturing Variables from the Environment
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Type Inference in Closures
Closures को आमतौर पर explicit parameter या return types annotated करने की ज़रूरत नहीं -- Rust इन्हें इस आधार पर infer करता है कि closure कैसे उपयोग होता है, closure syntax को compact रखते हुए।
उदाहरण: Type Inference in Closures
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Multi-line Closure Bodies
जब एक closure को एक से ज़्यादा expression चाहिए, इसकी body curly braces में एक पूरा block हो सकती है, बिल्कुल एक function body की तरह।
उदाहरण: Multi-line Closure Bodies
// 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));
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि closures अपने आप अपने आस-पास के scope से variables capture कर सकते हैं, regular
fnfunctions के विपरीत। - हर closure parameter पर अनावश्यक type annotations लिखना, जब Rust आमतौर पर इन्हें usage से infer कर सकता है।
- अनजान 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: