【发布时间】:2019-03-20 10:52:47
【问题描述】:
我想拥有:
- 具有
AppServices的AppState, - 这个
AppServices应该有服务IdService, -
IdService本身应该可以访问AppState。
以下是我提出的想法,但我无法正确完成生命周期标记。 ????
struct IdService<'a> {
name: String,
state: &'a AppState,
}
struct AppServices<'a> {
id: Option<&'a IdService>,
}
struct AppState<'a> {
services: &'a AppServices,
}
impl<'a> AppState<'a> {
pub fn new() -> Self {
AppState {
services: AppServices { id: None },
};
}
}
fn main() {
let mut state = AppState::new();
let id_service = IdService {
name: "test".to_string(),
state: state,
};
let services = AppServices {
id: Some(id_service),
};
state.services = services;
}
编译器输出:
error[E0106]: missing lifetime specifier
--> src/main.rs:3:16
|
3 | state: &'a AppState,
| ^^^^^^^^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/main.rs:6:20
|
6 | id: Option<&'a IdService>,
| ^^^^^^^^^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/main.rs:10:19
|
10 | services: &'a AppServices,
| ^^^^^^^^^^^ expected lifetime parameter
【问题讨论】: