rust - 无法理解生锈寿命冲突
问题描述
我正在做一个虚拟应用程序来掌握 Rust 概念。在做一个 XML 结构时,我得到了错误
由于需求冲突,无法推断函数调用中生命周期参数的适当生命周期
定义是
impl<'a> XmlFile<'a>
和
pub fn get_node<'b>(self, node: &'b [u8]) -> &'b [u8]
据我了解,Rust 编译器不喜欢在函数结束后可以删除返回变量,如果 XML 文件在不同的时间删除(因为它们有'a
生命'b
周期)。但如果我把同样的,我得到错误
生命周期 'a 已经在范围内
,所以我看不到解决错误的方法。
知道我缺少什么吗?我想我一定仍然缺乏一些 Rust 概念。
编辑:我的误解是添加导致问题的代码
#[allow(unused_parens)]
pub struct XmlFile<'a> {
last_open_node: &'a[u8],
last_published: String,
index_pos: u64,
content: &'a[u8],
}
impl<'a> XmlFile<'a> {
pub fn new<'b: 'a>(file: &'b [u8]) -> XmlFile<'b> {
let new_xml = XmlFile {
last_open_node: &[0: u8],
last_published: "".to_string(),
index_pos: 0,
content: file,
};
return new_xml;
}
pub fn get_node<'b: 'a>(&self, node: &'b [u8]) -> &'b [u8] {
let buf_index: u64 = 0;
let has_matched: bool = false;
self.index_pos = 0;
for c in self.content {
self.index_pos += 1;
if (c == &b'<') {
buf_index = self.index_pos;
while (c != &b' ') {
for b in node {
if b == &self.content[buf_index as usize] {
has_matched = true;
buf_index += 1
} else {
has_matched = false;
continue;
}
}
if has_matched {
while(self.content[buf_index as usize] != b'>'){
buf_index+=1;
}
let r = &self.content[self.index_pos as usize..buf_index as usize];
return r;
}
}
}
}
return &[0 : u8];
}
pub fn get_rss_version<'b:'a>(&self) -> Result<u64 , &'static str>{
let found_slice = Self::get_node(&self, "rss".as_bytes());
if(found_slice != &[0:u8]){
let version_value = Self::get_value(found_slice);
if(version_value.is_ok()){
return Ok(version_value.unwrap()) ;
}
else{
return Err("Couldn't retrieve version from tag");
}
}
else{
println!("Couldn't find tag <rss");
return Err("Couldn't find tag <rss");
}
}
}
解决方案
让我们看看你的签名get_node
:
pub fn get_node<'b: 'a>(&mut self, node: &'b [u8]) -> &'b [u8] { ... }
以及您在此方法中实际返回的内容:
let r = &self.content[self.index_pos as usize..buf_index as usize];
return r;
的签名get_node
表示此方法将返回 的子切片node
,但您实际上返回的是 的子XmlFile
切片content
。
该问题的一种解决方案是了解返回值不是 的一部分node
,而是self.content
. 因此,我们可以将签名更改为:
pub fn get_node<'b>(&mut self, node: &'b [u8]) -> &'a [u8] { ... }
在这种情况下,我们甚至可以完全省略手动指定生命周期:
pub fn get_node(&mut self, node: &[u8]) -> &[u8] { ... }
get_node
这是实际编译的方法的清理版本:
pub fn get_node(&mut self, node: &[u8]) -> &[u8] {
let mut buf_index: u64;
let mut has_matched: bool = false;
self.index_pos = 0;
for c in self.content {
self.index_pos += 1;
if c == &b'<' {
buf_index = self.index_pos;
while c != &b' ' {
for b in node {
if b == &self.content[buf_index as usize] {
has_matched = true;
buf_index += 1
} else {
has_matched = false;
continue;
}
}
if has_matched {
while self.content[buf_index as usize] != b'>' {
buf_index += 1;
}
let r = &self.content[self.index_pos as usize..buf_index as usize];
return r;
}
}
}
}
return &[0u8];
}
推荐阅读
- javascript - 在点击一定数量的页面后使用 cookie 显示弹出窗口
- docker - How to pass variables through docker-compose to Dockerfile
- python - How to create Case() at runtime?
- reactjs - redux-saga-test-plan put effects do not match, but payload of actual and expected are equal
- javascript - React Native WebView 中的身份验证
- jquery - Select2 4.0 使用带有 ajax 的 templateResult
- javascript - Lodash:如何使用 id 从数组中删除项目或嵌套项目?
- javascript - ASP.NET:使用 Text 属性作为 jQuery 事件的选择器
- python - Python 从 url 下载大型 xml 文件以获取前 10 个条目
- ios - iPhone X WKWebView scroll stuck