问题描述
在以下示例中,我很难找出 fn过滤器
函数的类型签名。
I'm having troubles figuring out the type signature of the fn filter
function in following example.
Node和Descendant定义仅用于语法。这并不意味着什么!
use std::iter::Filter;
#[derive(Clone)]
pub struct Node<'a> {
s: &'a str,
}
pub struct Descendants<'a>{
iter: Node<'a>
}
impl<'a> Iterator for Descendants<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Node<'a>> {
Some(Node {s: self.iter.s})
}
}
impl<'a> Node<'a> {
pub fn descendants(&self) -> Descendants<'a> {
Descendants{ iter: Node{s: self.s} }
}
pub fn filter(&self, criteria: &str) -> Filter<Descendants<'a>, fn(&'a Node<'a>)->bool > {
self.descendants()
.filter(|node| node.s == "meh")
}
}
fn main() {
let doc = Node{s: "str"};
}
我得到的错误如下:
<anon>:27:28: 27:34 error: the type of this value must be known in this context
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~
<anon>:27:21: 27:43 error: mismatched types:
expected `fn(&Node<'_>) -> bool`,
found `[closure <anon>:27:21: 27:43]`
(expected fn pointer,
found closure) [E0308]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~
<anon>:27:14: 27:44 error: type mismatch: the type `fn(&Node<'_>) -> bool` implements the trait `core::ops::FnMut<(&Node<'_>,)>`, but the trait `for<'r> core::ops::FnMut<(&'r Node<'_>,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:27:14: 27:44 error: type mismatch resolving `for<'r> <fn(&Node<'_>) -> bool as core::ops::FnOnce<(&'r Node<'_>,)>>::Output == bool`:
expected bound lifetime parameter ,
found concrete lifetime [E0271]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors
playpen: application terminated with error code 101
当我按照这个问题我试图替换 pub fn filter(& self,criteria:& str) - >过滤< Descendants<'>,fn(&'a Node<'>) - > bool>
,带 pub fn过滤器(& self,标准: & str) - > ()
我得到
When I as per this question Correct way to return an Iterator? I tried to replace pub fn filter(&self, criteria: &str) -> Filter<Descendants<'a>, fn(&'a Node<'a>)->bool >
with pub fn filter(&self, criteria: &str) -> ()
I get
<anon>:26:9: 27:44 error: mismatched types:
expected `()`,
found `core::iter::Filter<Descendants<'_>, [closure <anon>:27:21: 27:43]>`
我应该替换关闭
用?
或者,如果返回过滤器
太难而且挑剔,我该怎么写 Wrapper
for fn filter()
返回类型?
Alternatively, if it's too hard and finicky to return a Filter
, how do I write the Wrapper
for fn filter()
return type?
推荐答案
我清楚地记得几次之前已经回答了这个问题(我甚至在几分钟之前的)但我现在找不到链接,所以就这样了。
I clearly remember that this was answered before a few times (I even wrote about it in an answer a few minutes before) but I can't find a link now, so here it goes.
你的代码的问题是你使用一个闭包 filter()
参数:
The problem with your code is that you use a closure as filter()
argument:
.filter(|node| node.s == "meh")
Rust中的未装箱闭包是以匿名类型实现的当然,无法命名,因此有 no 方式来编写函数的签名,该函数返回使用闭包的迭代器。这就是你得到的错误信息:
Unboxed closures in Rust are implemented as anonymous types which, naturally, can't be named, so there is no way to write a signature of a function which returns an iterator which uses a closure. That's what the error message you're getting is about:
expected `fn(&Node<'_>) -> bool`,
found `[closure <anon>:27:21: 27:43]`
(expected fn pointer,
found closure) [E0308]
有几种解决方法,其中一种方法是使用特征对象:
There are several ways around this, one of them is to use trait objects:
pub fn filter<'b>(&'b self, criteria: &'b str) -> Box<Iterator<Item=Node<'a>+'b>>
{
Box::new(self.descendants().filter(move |node| node.s == criteria))
}
鉴于您的闭包具有非空的环境,这是您的代码工作的唯一方式。如果你的闭包没有捕获任何东西,你可以使用一个静态函数,其类型可以写出:
Given that your closure has a non-empty environment, this is the only way for your code to work. If your closure didn't capture anything, you could use a static function whose type can be written out:
pub fn filter(&self) -> Filter<Descendants<'a>, fn(&Node<'a>) -> bool> {
fn filter_fn<'b>(node: &Node<'b>) -> bool {
node.s == "meh"
}
self.descendants().filter(filter_fn)
}
这篇关于找出闭合的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!