When I pass the user_service.rs call user_dao.rs , it does not success call user_dao trait method in Dao, what happened? Do I should to change to the implementation method of struct? I'm a little confused between trait and struct. I usually look at trait as an interface.
UserDao::user_register(&a, &user_dto); Call not effective
This is my directory structure:
api
|_user_api.rs
service
|_user_service.rs
dao
|_user_dao.rs
|
main.rs
user_service.rs:
use crate::dao::user_dao::UserDao;
use crate::domain::dto::user::UserRegisterDto;
use crate::domain::entity::user::UserRegister;
pub fn save_user(user_dto: &UserRegisterDto) {
println!("userdto: {:?}", user_dto);
let a = UserRegister{
id: Some(String::from("55")),
user_name: Some(String::from("55")),
user_password: Some(String::from("55")),
name: Some(String::from("55")),
phone: Some(String::from("55")),
};
UserDao::user_register(&a, &user_dto);
}
The issue might be that user_register() returns a Future, but you aren't awaiting it. Does it work if you make save_user() an async fn and call UserDao::user_register(...).await;?
It's not enough to learn to rust. I have to look at cargo, because I don't know how to build like maven and gradle. After reading cargo, I find that can't do it again. I also need to look at async and await
Yes, for an async function call to take effect, you must await the returned Future. To do this, your service module either needs to be async itself, or explicitly block on the async runtime.
The #[async_trait] places a #[must_use] on the method, since it has no effect unless it is awaited. So you should be getting an unused_must_use warning from the compiler, unless you silenced the lint.