then I add it to main and try to call run(), but the run() can't be found
mod app;
fn main() {
let my_app = app::MyApp {};
my_app.run();
}
error[E0599]: no method named run found for struct MyApp in the current scope
--> src/main.rs:5:12
|
5 | my_app.run();
| ^^^ method not found in MyApp
I must add a 'use' for this trait in the main file: use crate::app::app_trait;
The trait 'app_trait' is defined as pub, why can't it be found in the main file?
Actually, I hope that user only know 'MyApp' and not care about the trait.
Traits are only consulted for method lookup if they are in scope — if there is a use for them or a trait bound. You cannot call a method from a trait without mentioning the trait (or a trait with it as a supertrait) somewhere in that file. pub only affects whether it's possible for the trait to be in scope.
If you want my_app.run() to work without mentioning the trait, you must define an inherent method. If you want, that inherent method can just forward to the trait method, or vice versa,
Why do you have that trait in the first place, then? Just implement your method directly for your type, there is no need to add any traits if you don't plan to use them.
So I hope user don't know about the trait and only care about 'MyApp'.
But I found new question after updating the code, I add a 'Factory' to create the app, and it will return a Box , then the main function could work and I don't need to add the "use crate::app::app_trait"
pub struct AppFactory;
impl AppFactory{
pub fn create() -> Box<dyn AppTrait>{
Box::new(MyApp1{})
// Maybe return MyApp2 or other
}
}
mod app;
fn main() {
let my_app = app::AppFactory::create();
my_app.run();
}
Besides use, traits can also be used in situations where they “obviously” should be:
the value is of a generic type bounded by that trait (T: AppTrait)
the value is a dyn AppTrait
In either of those cases, there aren't any other methods, and it is almost certain that the explicitly involved trait is what matters. So, the compiler makes it available regardless of whether it's used. (This is not a very well documented part of the language.)