- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个函数来转换以下形式的数据结构:
input = [("a", [1,2,3]), ("b", [4,5,6])]
output = [(a,1), (c,2) ..... (b,6)]
let foo=vec![('a', vec![1,2,3]), ('v', vec![2,3,4])];
let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
println!("{:?}",baz);
error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function
--> src/lib.rs:10:76
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| ^^^ - `a` is borrowed here
| |
| may outlive borrowed value `a`
|
note: closure is returned here
--> src/lib.rs:10:55
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( move |b|(a.0, b))).flatten().collect();
| ^^^^^^^^
error[E0382]: borrow of moved value: `a`
--> src/lib.rs:10:76
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| --- ^^^ - borrow occurs due to use in closure
| | |
| value moved here value borrowed here after partial move
|
= note: move occurs because `a.1` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
最佳答案
IntoIterator
使用并产生值。由于Vec
没有实现Copy
,因此当您调用a.1.into_iter()
时,它将被移动。您可以像这样克隆它:a.1.clone().into_iter()
另外,您还想使用move
关键字来获取闭包中a
的所有权。
let baz: Vec<(char, i32)> = foo
.into_iter()
.map(|a| a.1.clone().into_iter().map(move |b| (a.0, b)))
.flatten()
.collect();
println!("{:?}", baz);
// [('a', 1), ('a', 2), ('a', 3), ('v', 2), ('v', 3), ('v', 4)]
关于rust - Rust,在闭包内创建一个闭包,避免 “closure may outlive the current function”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002805/
我正在经历传说中的RFC 1214似乎我遗漏了一些重要的东西。 struct Foo; struct Bar { foo: &'a Foo } fn f(_: &'x Foo, _: &'y B
我的目标是拥有一个 Store其中包含各种 Entry项目。 Entry元素可以比商店本身生命周期更长,所以 Store保持 Vec ( playground ): struct Entry {
我试图将函数作为参数传递并在线程内调用它。这是我要运行的代码: use std::thread; pub struct Image { pub data: Vec, } pub trait I
我用 gtk-rs 构建了一个 GTK 应用程序。当我构建主窗口时,我想使用一些动态参数,例如窗口高度。我创建了一个包含所有此类设置的结构,并希望将其用作构建 UI 的函数的输入参数: fn main
我用 gtk-rs 构建了一个 GTK 应用程序。当我构建主窗口时,我想使用一些动态参数,例如窗口高度。我创建了一个包含所有此类设置的结构,并希望将其用作构建 UI 的函数的输入参数: fn main
我写了以下代码来测试闭包: fn main() { let captured_val = "a captured value".to_string(); let closure_par
我的 lib.rs 中有以下结构 pub enum ConfigurationSource { StringContent(String), FileContent(PathBuf)
当我尝试编译以下代码时: fn main() { (...) let mut should_end = false; let mut input = Input::new(ct
我正在尝试编写一个函数来转换以下形式的数据结构: input = [("a", [1,2,3]), ("b", [4,5,6])] 进入 output = [(a,1), (c,2) ..... (b
我的程序使用变量的内存地址作为唯一标识符。我知道这非常丑陋,但它是一种获取唯一标识符的非常轻量级的方法。 此模式仅在我将这些变量设为静态时有效,这样它们的唯一 ID(即地址)“永远”存在这意味着我有几
我有两个向量 itertools::MinMaxResult .我需要遍历第一个向量,对于每个元素,遍历第二个向量,检查第一个向量的最小值是否等于第二个向量的任何元素的最大值,反之亦然。这是我尝试过的
在下面的示例中,我有通过命令行参数设置的参数 verbose 和 data_source。 verbose 是一个 bool 标志,但 data_source 用于从一组可用函数中选择一个默认值,这些
我对 Rust 的了解还不够深,无法理解生命周期和闭包... 尝试使用 tokio-curl 将下载的数据收集到向量中: extern crate curl; extern crate futures
我想创建一个可以访问参数instanceContext的回调函数,但是我不确定如何修复该代码。 失败的代码是instanceContext.instances.len()。 我已经尝试过使用生命周期,
我想实现一个类似于调试 builders 的构建器由标准库定义。它们使用如下结构定义: struct DebugFoo { fmt: &'a mut std::fmt::Formatter }
我是一名优秀的程序员,十分优秀!