Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(fn_traits)]
- #![feature(unboxed_closures)]
- struct Closure {
- a: u32,
- b: u32
- }
- impl FnOnce<(u32,)> for Closure {
- type Output = u32;
- extern "rust-call" fn call_once(self, (c, ): (u32, )) -> Self::Output {
- self.a + self.b + c
- }
- }
- fn main() {
- use std::mem;
- let a = 1;
- let b = 2;
- let with_lambda = move |c: u32| a + b + c;
- let with_struct = Closure { a, b };
- let lambda_size = mem::size_of_val(&with_lambda);
- let struct_size =mem::size_of_val(&with_struct);
- assert_eq!(lambda_size, struct_size);
- assert_eq!(lambda_size, mem::size_of_val(&a) + mem::size_of_val(&b));
- assert_eq!(with_lambda(3), 6);
- assert_eq!(with_struct(3), 6);
- }
Add Comment
Please, Sign In to add comment