Created
March 11, 2025 22:40
-
-
Save malcolmgreaves/a6d17be0d1fecafc9cb0421c7d78e6bf to your computer and use it in GitHub Desktop.
Rust function parameter via generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn foo() -> impl Fn(i32) -> i32 { | |
|x| x + 1 | |
} | |
fn qux<F>(f: F) -> i32 | |
where F: Fn(i32) -> i32 { | |
f(10) | |
} | |
pub fn main() { | |
let f = foo(); | |
println!("foo()(10): {}", f(10)); | |
println!("from qux: f(10): {}", qux(&f)); | |
} | |
/* | |
Generated assembly: | |
------------------- | |
0000000100004080 <__ZN1x3qux17hec88419833e3d63fE>: | |
100004080: d100c3ff sub sp, sp, #0x30 | |
100004084: a9027bfd stp x29, x30, [sp, #0x20] | |
100004088: 910083fd add x29, sp, #0x20 | |
10000408c: aa0003e8 mov x8, x0 | |
100004090: 910023e0 add x0, sp, #0x8 | |
100004094: f90007e8 str x8, [sp, #0x8] | |
100004098: 52800141 mov w1, #0xa ; =10 | |
10000409c: 97ffff92 bl 0x100003ee4 <__ZN4core3ops8function5impls68_$LT$impl$u20$core..ops..function..Fn$LT$A$GT$$u20$for$u20$$RF$F$GT$4call17h6618343c809f89b5E> | |
1000040a0: b90007e0 str w0, [sp, #0x4] | |
1000040a4: 14000007 b 0x1000040c0 <__ZN1x3qux17hec88419833e3d63fE+0x40> | |
1000040a8: f9400be0 ldr x0, [sp, #0x10] | |
1000040ac: 9400d6d9 bl 0x100039c10 <dyld_stub_binder+0x100039c10> | |
1000040b0: f9000be0 str x0, [sp, #0x10] | |
1000040b4: aa0103e8 mov x8, x1 | |
1000040b8: b9001be8 str w8, [sp, #0x18] | |
1000040bc: 17fffffb b 0x1000040a8 <__ZN1x3qux17hec88419833e3d63fE+0x28> | |
1000040c0: b94007e0 ldr w0, [sp, #0x4] | |
1000040c4: a9427bfd ldp x29, x30, [sp, #0x20] | |
1000040c8: 9100c3ff add sp, sp, #0x30 | |
1000040cc: d65f03c0 ret | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment