Compiling playground v0.0.1 (/playground)
error[E0053]: method `fn_name` has an incompatible type for trait
--> src/lib.rs:8:19
|
8 | fn fn_name () {
| ^ expected `u32`, found `()`
|
note: type in trait
--> src/lib.rs:4:22
|
4 | fn fn_name () -> u32;
| ^^^
= note: expected fn pointer `fn() -> u32`
found fn pointer `fn()`
help: change the output type to match the trait
|
8 | fn fn_name () -> u32 {
| ++++++
For more information about this error, try `rustc --explain E0053`.
error: could not compile `playground` due to previous error
This error also shows you how to solve the problem: add the missing -> u32 to match the trait.
P.S.: Note that I used the todo! macro for creating the example, which I find quite helpful when providing examples or testing whether some code compiles.
Not everyone is a native English speaker, so it's normal that grammar or vocabulary can be messed up. In my opinion that's not a problem. My native language is German.
Feel free to ask for help here, even if your English isn't 100% correct. It can help, however, to try to be as precise as possible.
Getting the terminology right can also be tricky, even for English speakers. For example, I made a mistake here:
Structs have fields and methods, not variables (but I made that mistake as well and actually had to look that up in the reference).
P.S.: Did your question get answered?
P.P.S.: Of course traits have methods too. But you can also implement methods directly on the struct. Either is possible.
P.S.: In cases where the field is a non-Copy type, e.g. String, the type signature may look slightly different. In that case, the getter returns a reference usually, but may depend on the use case. For example:
When you do that Rust, basically, replaces s.fn_name with Trait::fn_name(s) (there are some complications, but the idea is like that).
Note how this doesn't change anything in the Struct itself. And when program is execute there are no “attachment” between trait and Struct. It all happens during compile time.