- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有以下签名的函数:
pub fn history<'a, I: IntoIterator<Item = &'a str>>(&self, _: I)
稍后,我有一个结构体,它有一个名为 main
的字段,它是一个盒装闭包。
main: box |args: &[&str], shell: &mut Shell| {
shell.history.history(args);
},
重要的部分是我正在调用我显示签名的函数,并将 &[&str]
作为参数。我收到以下编译错误:
src/main.rs:281:47: 281:54 error: type mismatch resolving `<&[&str] as core::iter::IntoIterator>::Item == &str`:
expected &-ptr,
found str [E0271]
src/main.rs:281 shell.history.history(args);
显然 &[&str]
不能用作 IntoIterator
。我尝试了 shell.history.history(args.into_iter());
并得到了类似的错误消息。
奇怪的是,shell.history.history(args.iter().map(|s|*s));
确实有效。不过,这似乎不是正确的解决方案。
最佳答案
我们来看看how IntoIterator
is implemented for slices :
impl<'a, T> IntoIterator for &'a [T]
type Item = &'a T
type IntoIter = Iter<'a, T>
fn into_iter(self) -> Iter<'a, T>
请注意 Item
被定义为对 T
的引用 , 其中T
是切片中项目的类型。因为你有一片 &str
,这意味着 Item
是&&str
.
您可以使用 .map(|s| *s)
,正如您所尝试的那样,取消对外部引用的引用并生成 &str
的迭代器.
另一种解决方案是概括您的 history
函数同时接受 I: IntoIterator<Item = &'a str>
和 I: IntoIterator<Item = &'b &'a str>
.为了做到这一点,我们需要一个特征同时 &'a str
和 &'b &'a str
实现。我们可以使用 AsRef<str>
为此(感谢 Vladimir Matveev 指出这一点):
pub fn history<I>(i: I)
where
I: IntoIterator,
I::Item: AsRef<str>,
{
for s in i {
println!("{}", s.as_ref());
}
}
fn main() {
history(&["s"]);
}
关于rust - 将切片作为 IntoIterator 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35144386/
我有一个具有不同变体可能性的枚举,如下所示: pub enum Enum { Empty, Single(Struct), Multi(Vec), } 我想创建典型的三个迭代函数来获取 E
IntoIterator 类型的特征对象是否可以装箱并保存在结构中?我有一种情况,我想存储一个可以变成迭代器的对象向量。我对此的尝试是代码 struct Foo { foo: Vec>>,
我正在尝试构建一个二叉树并编写一个迭代器来遍历树中的值。在为我的树节点实现 IntoIterator 特性时,我遇到了生命周期问题 src\main.rs:43:6: 43:8 error: the
我想要一个函数,接受&IntoIterator ,所以我可以同时传递给它 &Vec和迭代器的适配器结构(如 Map 、 Filter 和任何其他我相信都实现了 IntoIterator ) 所以我有一
首先,我想要一个接受泛型集合但不消耗它的函数。也就是说,我们有一个函数 non_consuming . fn non_consuming(_: &'a I) where &'a I: std::ite
我试图在通用图上实现一些图算法。为此,我定义了两个图形特征,它们将返回一个通用特征(具有集合操作)SetGraph 或一个用于迭代节点 NeighborhoodIteratorGraph 的 Into
我已经掌握了 Rust 生命周期的基础知识以及如何在其中使用迭代器,但仍然难以定义一个函数,该函数接受一个可迭代的元组并返回一个也在堆上分配的可迭代的元组。 (我知道“可迭代”在 Rust 中没有任何
我有一个具有以下签名的函数: pub fn history>(&self, _: I) 稍后,我有一个结构体,它有一个名为 main 的字段,它是一个盒装闭包。 main: box |args: &[
Rust 似乎并不区分特征的不同实现,仅当它们在关联类型上有所不同时。 如何在所有类型的集合/迭代器上实现一个方法,但对它们包含的每个具体类型都有特定的实现? error: conflicting i
我有一个函数 f应该这样称呼: struct S; let s = &S; f(&[(s, &[0.1, 0.2, 0.3]), (s, &[0.5, 0.6])]); 为了一个方便的界面,我想要f处
我正在玩迭代器,我一直在使用 Flatten更好地理解迭代器是如何工作的。这个例子: assert_eq!(vec![1, 2].iter().flatten().count(), 2); 不编译并给
我正在尝试编写一个类似 Vec 的具有固定大小的对象,以便在堆栈上使用。 这是我要完成的在线类(class)的一部分 Assignment 1, Phase 2A . 我在实现 IntoIterato
我有这个例子: struct Foo { data: Vec } impl Foo { fn get_list(&self) -> &Vec { &self.data
我正在编写一个以通用方式实现前缀树的容器(键和值是通用参数)。由于此数据结构的性质,我需要具有迭代器的 key 。 基本上符合this question IntoIterator 特征是造成这种可能性
我正在定义一个接受 i: &I 的特征范围。我想用这个 i for 中的值循环。 例如: struct Test; trait Bar { fn bar(&self, i: &I); } im
我有一个结构,其属性类型为 HashMap。我想为此结构实现 IntoIterator 特性,以便我可以迭代它的 HashMap 属性。问题是我遇到了一生的 hell : pub struct Pro
我试图遍历字符串向量的一个子部分,即 Vec 的子切片.在每次迭代中,我想将字符串作为切片传递给函数。 我没注意到 Vec::get返回 Option ,并认为我可以直接迭代返回值: fn take_
如何为以下结构实现 Iterator 和 IntoIterator 特征? struct Pixel { r: i8, g: i8, b: i8, } 我已经尝试了以下各种形式
自关联类型 IntoIter的IntoIterator trait,实现了 Iterator trait,这不足以推断关联类型 Item ? 最佳答案 Why does the IntoIterato
我想为任何通用集合实现一个小的有向图特征 C实现 std::ops::Index和 std::iter::IntoIterator .我希望集合代表图形的节点。每个节点都由其在 C 中的索引表示,这可
我是一名优秀的程序员,十分优秀!