- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个包含字符串索引和字符串中每个字符的向量。
在下面的例子中,0 J, 0 a, 0 n ... 1 J, 1i, ...
fn main() {
let names = vec!["Jane", "Jill", "Jack", "Johne"];
let name3 = names.iter().enumerate().flat_map(|(i, name)| {
let letters = name.chars();
let mut is = Vec::new();
for _k in 0..name.len() {
is.push(i);
}
is.iter().zip(letters).collect::<Vec<_>>()
});
for i in name3 {
println!("{:?}", i);
}
}
这给了我错误
error[E0597]: `is` does not live long enough
--> src/main.rs:9:9
|
9 | is.iter().zip(letters).collect::<Vec<_>>()
| ^^ borrowed value does not live long enough
10 | });
| - `is` dropped here while still borrowed
...
14 | }
| - borrowed value needs to live until here
我不明白这是怎么回事。我已经收集了 is
值。
奇怪的是,如果我翻转 letters
和 is
,它会起作用。
letters.zip(is)
最佳答案
调用 is.iter()
返回对 is
中的值的引用。您的最终迭代器类型试图返回这些引用,除了保存值的 Vec
已经被释放。
最简单的解决方法是切换到 into_iter
,它拥有 Vec
及其所有值的所有权。更有效的解决方法是完全避免创建 Vec
:
fn main() {
let names = vec!["Jane", "Jill", "Jack", "Johne"];
let name3 = names
.iter()
.enumerate()
.flat_map(|(i, name)| name.chars().map(move |c| (i, c)));
for i in name3 {
println!("{:?}", i);
}
}
if I flip
letters
andis
, it works.
是的,zip
采用实现 IntoIterator
的值.通过翻转参数,您最终会在 Vec
上隐式调用 into_iter
。如果您执行 letters.zip(&is)
,您会遇到同样的意外行为。
关于dictionary - flat_map 在借来的时候丢在这里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515225/
我正在尝试编写一个包含字符串索引和字符串中每个字符的向量。 在下面的例子中,0 J, 0 a, 0 n ... 1 J, 1i, ... fn main() { let names = vec
我一整天都在编写以下代码,(here 是围栏) /// The rule that moves state from one to another. /// /// `S` - the type pa
你好!我正在尝试完成我的第一个 RxPY 项目,但我遇到了一些问题 了解 Python 中 flat_map 的行为。 在这个项目中,有一个从生成器(Kafka 消费者)创建的 Observable。
给定结构的集合(向量/切片)。如何在每个结构的某些字段上创建组合的迭代器? 以下是使用flat_map的具体尝试: struct Game { home_team: u8, away_
我知道我们可以使用 collect移动 Result从内到外,例如: fn produce_result(my_struct: &MyStruct) -> Result; let my_results
这个问题在这里已经有了答案: Conditionally iterate over one of several possible iterators (4 个答案) 关闭 2 年前。 是否可以在
有没有办法在不切换到使用 iter 而不是 into_iter 的情况下完成这项工作? let strings: Vec = vec!["1 2".to_string(), "3 4".to_stri
我的问题的以下简化/抽象版本: fn main() { let arr = vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]; let size = a
我的问题的以下简化/抽象版本: fn main() { let foo_selectors = vec![0, 1]; let foos: Vec = foo_selectors
我正在研究 Project Euler 96自学 Rust。我编写了这段代码来读取文件并将其转换为整数向量 ( Playground )。 let file = File::open(&args[1]
给定 foo 的定义: let foo = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]; 我希望能够编写这样的代码: let result: V
在处理一些遗留代码时,我遇到了内存问题,这主要是(我相信)由于广泛使用 STL map (特别是“ map 中的 map ”)。 我正在将 Boost flat_map 视为一种可能的解决方案。有没有
我正在尝试生成这样的序列:1,2,3,4,5,6,7,8,9,1,0,1,1,1,2... fn main() { let iter = (1..).flat_map(|j| j.to_str
为什么在闭包上没有类型注释的情况下可以编译? fn transform(input: &Vec>) { input.iter().flat_map(|words| words.iter());
我认为当你有一个可变长度的散列数组并且你想单独处理每个散列时,flat_map 很棒: data = [ {'apple' => 'fruit'}, {'carrot' => 'vegetab
编辑:因为我用错误的例子写了问题并且没有描述我的问题,我会再做一次! 在我看来,#flat_map 尽管是 Enumerator::Lazy 类的一部分,但它本身并不是非常可枚举的。 这个例子可以正常
我认为我对生命周期有很好的理解,但我读到的内容与编译器在闭包方面所说的不一致;) 我有一个带有签名的函数: fn split_to_words(content: &str) -> Vec 使用 for
我希望能够重复一个过程,在该过程中,我们正在迭代的集合被更改 n 次。 n 仅在运行时已知,可以由用户指定,因此我们不能将其硬编码到类型中。 通过 collect 在迭代之间使用中间数据结构的方法是可
我正在尝试创建一个管道,以使用 TensorFlow Dataset API 和 Pandas 读取多个 CSV 文件。但是,使用 flat_map 方法会产生错误。但是,如果我使用 map 方法,我
我正在使用数据集 API,读取数据如下: dataset = tf.data.TFRecordDataset(filename, compression_type="GZIP") dataset =
我是一名优秀的程序员,十分优秀!