结构体内枚举的生命周期参数 [英] Lifetime parameters for an enum within a struct
问题描述
我不明白为什么我会遇到这种结构类型的错误
枚举细胞< a> {
Str(&'a str),
Double(&'a f32),
}
struct MyCellRep<'& {
value:&'a Cell,
ptr:* const u8,
}
impl MyCellRep {
fn new_from_str(s:& str){
MyCellRep {value:Cell :: Str(& s),ptr:new_sCell(CString :: new(& s))}
}
fn new_from_double(d:& f32){
MyCellRep {value:Cell :: Double(& d),ptr:new_dCell(& d)}
}
}
我收到错误
14:22错误:生命周期参数错误:预期1,发现0 [E0107]
src\lib.rs:14 value:& 一个单元格,
所以我也尝试了
< pre class =lang-rust prettyprint-override>
struct MyCellRep<'a> {
value:&'a Cell +'a,
ptr:* const u8,
}
但得到
14:22错误: +左侧的路径,而不是'&'一个单元格
I假设单元格
的生命周期应为 MyCellRep
和 Cell :: Str
和 Cell :: Double
应至少具有 Cell
的生命周期。
最终我可以做的只是说
let x = MyCellRef :: new_from_str(foo);
let y = MyCellRef :: new_from_double(123.0);
更新
我想添加,通过更改单元格定义,其余代码也应该更改为以下任何人搜索答案。
pub枚举Cell<'a> {
Str(&'a str),
Double(&'a f32),
}
struct MyCellRep<'& {
value:Cell<'a>,// Ref to enum
ptr:* const u8,//指向c struct
}
impl <一个> MyCellRep<一> {
fn from_str(s:&a''a str) - > DbaxCell<一> {
MyCellRep {value:Cell :: Str(& s),ptr:unsafe {new_sCell(CString :: new(s).unwrap())}}
}
fn from_double(d:&'a f32) - > DbaxCell {
MyCellRep {value:Cell :: Double(& d),ptr:unsafe {new_dCell(* d)}}
}
}
我对Rust的喜欢就像OCaml一样,如果它编译工作:)
你(可以理解)误解了错误信息:
14:22错误:生命周期参数错误:预期1,发现0 [E0107]
src\lib.rs:14 value:& 'a Cell,
你以为但是我提供了一生的参数!它是'一个
!但是,编译器试图告诉您,您没有为 Cell提供一个生命周期参数(而不是它的引用):
细胞和LT;'一>
I don't understand why I get an error with this type of structure
enum Cell <'a> {
Str(&'a str),
Double(&'a f32),
}
struct MyCellRep<'a> {
value: &'a Cell,
ptr: *const u8,
}
impl MyCellRep{
fn new_from_str(s: &str) {
MyCellRep { value: Cell::Str(&s), ptr: new_sCell(CString::new(&s)) }
}
fn new_from_double(d: &f32) {
MyCellRep { value: Cell::Double(&d), ptr: new_dCell(&d) }
}
}
I get the error
14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src\lib.rs:14 value : & 'a Cell ,
So I tried also
struct MyCellRep<'a> {
value: &'a Cell + 'a,
ptr: *const u8,
}
but got
14:22 error: expected a path on the left-hand side of `+`, not `&'a Cell`
I presume the Cell
should have the lifetime of MyCellRep
, and Cell::Str
and Cell::Double
should at least have the lifetime of the Cell
.
Eventually what I was to be able to do is just say
let x = MyCellRef::new_from_str("foo");
let y = MyCellRef::new_from_double(123.0);
Update I would like to add, by changing the Cell definition, the rest of the code should also change to the following for anyone else searching answers.
pub enum Cell<'a> {
Str(&'a str),
Double(&'a f32),
}
struct MyCellRep<'a> {
value: Cell<'a>, // Ref to enum
ptr: *const u8, // Pointer to c struct
}
impl<'a> MyCellRep<'a> {
fn from_str(s: &'a str) -> DbaxCell<'a> {
MyCellRep { value: Cell::Str(&s) , ptr: unsafe { new_sCell(CString::new(s).unwrap()) } }
}
fn from_double(d: &'a f32) -> DbaxCell {
MyCellRep{ value: Cell::Double(&d) , ptr: unsafe { new_dCell(*d) } }
}
}
What I love about Rust is just like OCaml, if it compiles it works :)
You (understandably) misinterpreted the error message:
14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src\lib.rs:14 value : & 'a Cell ,
You thought "But I provided the lifetime parameter! It is 'a
!" However, the compiler is trying to tell you that you did not provide a lifetime parameter for Cell (not the reference to it):
Cell<'a>
这篇关于结构体内枚举的生命周期参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!