struct Foo {
bar:fn(&Foo)
}
impl Foo {
fn bar1(&self) {
println!("Hello from bar1");
}
fn bar2(&self) {
println!("Hello from bar2");
}
}
fn main () {
let foo1 = Foo {
bar:Foo::bar1
};
let foo2 = Foo {
bar:Foo::bar2
};
foo1.bar(&foo1);
foo2.bar(&foo2);
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `bar` found for struct `Foo` in the current scope
--> src/main.rs:22:10
|
2 | struct Foo {
| ---------- method `bar` not found for this struct
...
22 | foo1.bar(&foo1);
| ^^^ field, not a method
|
help: to call the function stored in `bar`, surround the field access with parentheses
|
22 | (foo1.bar)(&foo1);
| + +
help: there is a method with a similar name
|
22 | foo1.bar1(&foo1);
| ~~~~
error[E0599]: no method named `bar` found for struct `Foo` in the current scope
--> src/main.rs:23:10
|
2 | struct Foo {
| ---------- method `bar` not found for this struct
...
23 | foo2.bar(&foo2);
| ^^^ field, not a method
|
help: to call the function stored in `bar`, surround the field access with parentheses
|
23 | (foo2.bar)(&foo2);
| + +
help: there is a method with a similar name
|
23 | foo2.bar1(&foo2);
| ~~~~
For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to 2 previous errors
This code paraphrases some actual code where I have a struct that gets instantiated for two slightly different cases. In particular, there are a pair of similar but slightly different functions that need to be called in each of the cases, with the struct instance as an argument.
The advice given in the error message does work -- adding the parens results in error-free compilation and the code runs correctly. My question, though, is where is this documented? I've searched, but cannot find the justification or explanation for why this works. It's certainly possible it's there and I missed it.
I'd like to understand the language principle that applies here. It would also be great if the error message from the compiler, while helpful as it is, gave a hint as to why this works.
One addition: this code also produces the two E0599s:
struct Foo {
bar:fn(&Foo)
}
fn bar1(foo:&Foo) {
println!("Hello from bar1");
}
fn bar2(foo:&Foo) {
println!("Hello from bar2");
}
fn main () {
let foo1 = Foo {
bar:bar1
};
let foo2 = Foo {
bar:bar2
};
foo1.bar(&foo1);
foo2.bar(&foo2);
}
From the compiler:
Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `bar` found for struct `Foo` in the current scope
--> src/main.rs:22:10
|
2 | struct Foo {
| ---------- method `bar` not found for this struct
...
22 | foo1.bar(&foo1);
| ^^^ field, not a method
|
help: to call the function stored in `bar`, surround the field access with parentheses
|
22 | (foo1.bar)(&foo1);
| + +
error[E0599]: no method named `bar` found for struct `Foo` in the current scope
--> src/main.rs:23:10
|
2 | struct Foo {
| ---------- method `bar` not found for this struct
...
23 | foo2.bar(&foo2);
| ^^^ field, not a method
|
help: to call the function stored in `bar`, surround the field access with parentheses
|
23 | (foo2.bar)(&foo2);
| + +
For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to 2 previous errors
This is very odd, in my opinion, since no methods have been declared with impl and the word 'self' does not appear. It seems to think lines 22 and 23 are attempted method calls because they are in x.y form where x is a struct instance. And then, of course, the question of why (x.y) turns a field reference into a method call here is even more of a mystery than the first case, at least to me.
Thanks in advance for any help provided.