![](/img/trans.png)
[英]how do I find/select image height from an img I created within a jQuery function inside that function?
[英]How do I assign a lifetime when returning the result of document::find from the select crate?
我在定义一个函数时遇到了麻烦,该函数从select crate,v0.2.2返回Node
的向量。 我一直在添加这个函数,因为我已经通过错误消息(在线其他问题的帮助),但我无法弄清楚如何将'a
生命周期变量赋值给返回值:
extern crate select;
use select::document::Document;
use select::predicate::*;
fn elems_by_class<'a, Node>(document: &'a Document, class: &str) -> Vec<Node<>>
where Vec<Node>: std::iter::FromIterator<select::node::Node<'a>>
{
document.find(Attr("class", class)).iter().collect::<Vec<Node<>>>()
}
fn main() {}
我得到的错误是
error: borrowed value does not live long enough
--> src/main.rs:9:5
|
9 | document.find(Attr("class", class)).iter().collect::<Vec<Node<>>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value created here
10 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the block at 8:0...
--> src/main.rs:8:1
|
8 | {
| ^
如何为函数调用分配'a
生命周期? 我尝试(不成功)使用变量,但读取在函数体内创建的变量可能会导致问题,因此放弃了这种方法。 我是否在借用洞中挖了太多自己,是否应该以更简单的方式定义此功能?
您的核心问题是您已经定义了一个隐藏真实 Node
的泛型类型:
fn elems_by_class<'a, Node>(document: &'a Document, class: &str)
// ^^^^ -- no!
这应该是Expected类型参数的副本,找到了u8,但是type参数是u8 。
但是,选择库(版本0.2.2和0.3.0)似乎有一个错误:
impl<'a> Selection<'a> {
fn iter(&'a self) -> Iter<'a>;
}
这会强制迭代器返回的值具有与Selection
结构相关联的生命周期,而不是Document
。
现在似乎已经解决了这个问题:
impl<'a> Selection<'a> {
pub fn iter<'sel>(&'sel self) -> Iter<'sel, 'a>;
}
但修复程序还没有发布,所以你不能做任何事情,除了让维护者发布新版本或者你可以选择使用git仓库中的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.