Skip to content

Commit a590d0b

Browse files
committed
closue
1 parent 4fcaef9 commit a590d0b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rust/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,25 @@ fn macro_test() {
309309
assert_eq!(sample_macro!(1, 2), 3);
310310
}
311311

312+
#[test]
313+
fn closue() {
314+
// Closures are like lambda expressions in C++
315+
316+
// Closures can capture external objects (constants, variables)
317+
let one = 1;
318+
319+
// Closure which is bound to reference (name)
320+
let closure_annotated = |arg: i32| -> i32 { arg + one };
321+
assert_eq!(closure_annotated(1), 2);
322+
323+
// Annotation and block are optional
324+
let closure_inferred = |arg| arg + one;
325+
assert_eq!(closure_inferred(1), 2);
326+
327+
// anonymous closure
328+
assert_eq!((|arg| arg + one)(1), 2);
329+
}
330+
312331
#[test]
313332
#[ignore]
314333
fn failing_test() {

0 commit comments

Comments
 (0)