本文介绍了生命周期参数数量错误:预期为 1,发现为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难理解 Rust 中的指针.此代码无法编译,因为:生命周期参数的数量错误:预期为 1,找到 0 [E0107]
.这是什么意思?
I'm having a really hard time fathoming pointers in Rust. This code won't compile because: wrong number of lifetime parameters: expected 1, found 0 [E0107]
. What does that mean?
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)
}
推荐答案
这意味着您对结构的定义包括一个类型参数,因此,您的函数也必须创建一个类型参数.
It's saying that your definition for the struct includes a type parameter and, therefore, so must your function that creates one.
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)
}
这篇关于生命周期参数数量错误:预期为 1,发现为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!