我很难在Rust看到指针。此代码不会编译,因为:wrong number of lifetime parameters: expected 1, found 0 [E0107]
。这是什么意思?
struct Planet<'a> {
name: &'a str,
radius_km: i32,
surface_area_km2: i64,
orbital_period_days: i32,
distance_from_sun: i64
}
fn mercury() -> Planet {
Planet {
name: "Mercury",
radius_km: 2_440,
surface_area_km2: 74_800_000,
orbital_period_days: 88,
distance_from_sun: 57_910_000
}
}
fn main() {
let mercury = Box::new(mercury());
println!("{}",mercury.name)
}
答案 0 :(得分:7)
它说你的结构定义包含一个类型参数,因此你的函数也必须创建一个。
struct Planet<'a>{
name : &'a str,
radius_km: i32,
surface_area_km2: i64,
orbital_period_days: i32,
distance_from_sun: i64
}
fn mercury<'a>()->Planet<'a>{
Planet{
name:"Mercudy",
radius_km: 2_440,
surface_area_km2: 74_800_000,
orbital_period_days: 88,
distance_from_sun: 57_910_000
}
}
fn main(){
let planet = mercury();
println!("{}", planet.name)
}