我正在尝试将 a 转换str
为 aReader
并努力失败。
下面的代码所做的是从str
使用 with_str_reader中提取 Reader
use std::io::{with_str_reader,Reader};
pub struct ReadSource<'self> {
priv source: &'self Reader
}
impl<'self> ReadSource<'self> {
fn from_str(data: &'self str) -> ReadSource<'self>{
let r = std::io::with_str_reader(data, |reader| { reader });
ReadSource{
source : r
}
}
}
fn main() {
//Ignore
}
运行时我得到的是以下内容
test.rs:12:21: 12:22 error: cannot root managed value long enough
test.rs:12 source : r
^
test.rs:9:54: 14:5 note: managed value would have to be rooted for the lifetime &'self as defined on the block at 9:54...
test.rs:9 fn from_str(data: &'self str) -> ReadSource<'self>{
test.rs:10 let r = std::io::with_str_reader(data, |reader| { reader });
test.rs:11 ReadSource{
test.rs:12 source : r
test.rs:13 }
test.rs:14 }
我知道我必须用自己的生命来铸造一些东西,但我不确定是什么?!