2025-06-29 23:13:37 +05:00
|
|
|
//! Test basic closure syntax and usage with generic functions.
|
|
|
|
|
//!
|
|
|
|
|
//! This test checks that closure syntax works correctly for:
|
|
|
|
|
//! - Closures with parameters and return values
|
|
|
|
|
//! - Closures without parameters (both expression and block forms)
|
|
|
|
|
//! - Integration with generic functions and FnOnce trait bounds
|
2012-06-29 18:09:56 -07:00
|
|
|
|
2025-06-29 23:13:37 +05:00
|
|
|
//@ run-pass
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2025-06-29 23:13:37 +05:00
|
|
|
fn f<F>(i: isize, f: F) -> isize
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(isize) -> isize,
|
|
|
|
|
{
|
|
|
|
|
f(i)
|
|
|
|
|
}
|
2012-06-29 18:09:56 -07:00
|
|
|
|
2025-06-29 23:13:37 +05:00
|
|
|
fn g<G>(_g: G)
|
|
|
|
|
where
|
|
|
|
|
G: FnOnce(),
|
|
|
|
|
{
|
|
|
|
|
}
|
2012-06-29 18:09:56 -07:00
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2025-06-29 23:13:37 +05:00
|
|
|
// Closure with parameter that returns the same value
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(f(10, |a| a), 10);
|
2025-06-29 23:13:37 +05:00
|
|
|
|
|
|
|
|
// Closure without parameters - expression form
|
|
|
|
|
g(|| ());
|
|
|
|
|
|
|
|
|
|
// Test closure reuse in generic context
|
2013-11-21 17:23:21 -08:00
|
|
|
assert_eq!(f(10, |a| a), 10);
|
2025-06-29 23:13:37 +05:00
|
|
|
|
|
|
|
|
// Closure without parameters - block form
|
|
|
|
|
g(|| {});
|
2012-06-29 18:09:56 -07:00
|
|
|
}
|