2022-06-20 15:30:34 -07:00
|
|
|
static FOO: fn() = || assert_ne!(42, 43);
|
|
|
|
static BAR: fn(i32, i32) = |a, b| assert_ne!(a, b);
|
2017-02-28 12:35:00 +01:00
|
|
|
|
2018-08-27 17:08:42 +02:00
|
|
|
// use to first make the closure FnOnce() before making it fn()
|
2022-06-20 15:30:34 -07:00
|
|
|
fn force_once0<R, F: FnOnce() -> R>(f: F) -> F {
|
|
|
|
f
|
|
|
|
}
|
|
|
|
fn force_once1<T, R, F: FnOnce(T) -> R>(f: F) -> F {
|
|
|
|
f
|
|
|
|
}
|
|
|
|
fn force_mut0<R, F: FnMut() -> R>(f: F) -> F {
|
|
|
|
f
|
|
|
|
}
|
|
|
|
fn force_mut1<T, R, F: FnMut(T) -> R>(f: F) -> F {
|
|
|
|
f
|
|
|
|
}
|
2018-08-27 17:08:42 +02:00
|
|
|
|
2017-02-28 12:35:00 +01:00
|
|
|
fn main() {
|
|
|
|
FOO();
|
|
|
|
BAR(44, 45);
|
|
|
|
let bar: unsafe fn(i32, i32) = BAR;
|
|
|
|
unsafe { bar(46, 47) };
|
2019-05-30 10:58:30 +02:00
|
|
|
let boo: &dyn Fn(i32, i32) = &BAR;
|
2017-02-28 12:35:00 +01:00
|
|
|
boo(48, 49);
|
2018-08-27 17:08:42 +02:00
|
|
|
|
2022-06-20 15:30:34 -07:00
|
|
|
let f: fn() = || {};
|
2018-08-27 17:08:42 +02:00
|
|
|
f();
|
2022-06-20 15:30:34 -07:00
|
|
|
let f = force_once0(|| {}) as fn();
|
2019-11-27 21:50:20 +01:00
|
|
|
f();
|
2022-06-20 15:30:34 -07:00
|
|
|
let f = force_mut0(|| {}) as fn();
|
2019-11-25 15:01:05 +01:00
|
|
|
f();
|
|
|
|
|
|
|
|
let g: fn(i32) = |i| assert_eq!(i, 2);
|
|
|
|
g(2);
|
2019-11-27 21:50:20 +01:00
|
|
|
let g = force_once1(|i| assert_eq!(i, 2)) as fn(i32);
|
|
|
|
g(2);
|
|
|
|
let g = force_mut1(|i| assert_eq!(i, 2)) as fn(i32);
|
2019-11-25 15:01:05 +01:00
|
|
|
g(2);
|
2017-02-28 12:35:00 +01:00
|
|
|
}
|