Printing Time @WaffleLapkin @BoxyUwU
struct Foo {
func: fn(),
}
fn bar(foo: Foo) {
foo.func();
}
Solution
error[E0599]: no method named `func` found for struct `Foo` in the current scope
--> examples/misc_3.rs:7:9
|
2 | struct Foo {
| ---------- method `func` not found for this struct
...
7 | foo.func();
| ^^^^ field, not a method
|
help: to call the function pointer stored in `func`, surround the field access with parentheses
|
7 | (foo.func)();
| + +
For more information about this error, try `rustc --explain E0599`.
error: could not compile `code` (example "misc_3") due to 1 previous error
There is a syntactic difference between a method call and a normal call. expr.identifier() is always a method call and Foo does not have a method called func. To call the function stored in a field you need to add parenthesis:
fn bar(foo: Foo) {
(foo.func)();
}
Note that the same problem does not apply to tuples and tuple structs, because you can't name a method with an integer identifier. The following example compiles and prints "ferrisUwu" when run:
struct Foo(fn());
fn print_heheh() {
println!("ferrisUwu")
}
fn main() {
let foo = Foo(print_heheh);
foo.0();
}