- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不幸的是,我在管理 Rust 中与字符串和结构相关的生命周期时遇到了很多麻烦。
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde_json::Value;
use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use std::fs;
use std::cell::RefCell;
#[derive(Serialize, Deserialize, Debug)]
struct Song {
artist: String,
}
struct SongEntry {
date: &'static str,
song: &'static Song,
}
fn main() {
let paths = fs::read_dir("./charts/").unwrap();
let fileContents = paths.map(| path | {
let p = path.unwrap().path();
let file = File::open(&p).unwrap();
let v: Vec<Song> = serde_json::from_reader(file).unwrap();
return v.iter().map(move | song | {
let date = p.file_stem().unwrap().to_str().unwrap();
return SongEntry {
song: song,
date: date,
};
})
});
}
我在这里尝试了许多管理内存的变体,但似乎只是将一个错误换成另一个错误。
目的是遍历目录中的 JSON 文件,解析它们,并组装一个包含日期(来自文件名)和内容(来自已解析的 JSON)的对象向量。
到目前为止,我已尝试在内部 map
内或外部声明 date
,尝试使用 Arc
来管理 >date
变量,尝试使用和不使用 move
关键字的内部循环。
但是,我就是找不到一种方法让这些 map
方法中的变量绑定(bind)持续适当的时间。任何帮助将不胜感激。
当前产生的错误是:
Compiling kanye v0.1.0 (file:///Users/tmcw/src/sandbox/kanye)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:29:26
|
29 | let date = p.file_stem().unwrap().to_str().unwrap();
| ^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime as defined on the body at 28:42...
--> src/main.rs:28:43
|
28 | return v.iter().map(move | song | {
| ___________________________________________^ starting here...
29 | | let date = p.file_stem().unwrap().to_str().unwrap();
30 | | return SongEntry {
31 | | song: song,
32 | | date: date,
33 | | };
34 | | })
| |_________^ ...ending here
note: ...so that closure can access `p`
--> src/main.rs:29:24
|
29 | let date = p.file_stem().unwrap().to_str().unwrap();
| ^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that reference does not outlive borrowed content
--> src/main.rs:32:23
|
32 | date: date,
| ^^^^
error: aborting due to previous error
error: Could not compile `kanye`.
To learn more, run the command again with --verbose.
元问题:这是在 Rust 中对 map
的适当使用,还是我应该改用普通迭代?我之前也尝试过迭代,但也被内存检查阻碍了。
最佳答案
如果您想将引用存储在结构中,那么这些引用必须指向拥有比该结构更长寿的所有者的对象。此外,&'static
引用必须引用在整个程序执行期间有效的对象。 date
和 song
字段都不成立。
在这里,SongEntry
结构应该只拥有 date
和 song
对象。
struct SongEntry {
date: String,
song: Song,
}
对于date
字段,只需将字符串切片(&str
) 转换为一个拥有的字符串(String
) 与to_string
方法。对于 song
字段,您需要从向量 v
中移动所有权。然而,Vec::iter
只产生对其项目的借用引用。您必须使用 Vec::into_iter
相反,它直接返回值,并在此过程中消耗向量。
fn main() {
let paths = fs::read_dir("./charts/").unwrap();
let file_contents = paths.map(|path| {
let p = path.unwrap().path();
let file = File::open(&p).unwrap();
let v: Vec<Song> = serde_json::from_reader(file).unwrap();
v.into_iter().map(move |song| {
let date = p.file_stem().unwrap().to_str().unwrap().to_string();
SongEntry {
date: date,
song: song,
}
})
});
}
此时,file_contents
是 SongEntry
对象迭代器的迭代器(外部迭代器迭代文件,内部迭代器迭代文件中的条目)。如果您想直接拥有 SongEntry
对象的迭代器,请使用 flat_map
而不是 paths
上的 map
。然后您可以使用 collect
如有必要,在最终迭代器上将结果收集到一个向量中(如果您只打算对结果进行一次迭代,请不要使用 collect
!)。
关于json - Rust 变量 'does not live long enough' 和 'conflicting requirements',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464230/
我找到了long int long和 int long long可以编译为变量类型。 long int long有什么区别吗, int long long , long long和 long long
我无法找出为什么“加密”函数仍然将“消息”读取为字符串,尽管我已经使用不同的方法将数据类型更改为字节。 错误消息是“Prince 类型中的方法 Encrypt(long, long, long, lo
这个问题在这里已经有了答案: Is "long long" = "long long int" = "long int long" = "int long long"? (4 个答案) 关闭 6 年
我正在从 Java 过渡到 C++,并且对 long 数据类型有一些疑问。在 Java 中,要保存大于 232 的整数,您只需编写 long x;。但是,在 C++ 中,long 似乎既是数据类型又是
clang-tidy 12.0.1 报告了一个相当奇怪的警告。在以下代码中: #include int main() { std::vector v1; const auto a =
我创建了一个 pair 和 long long int 的映射 - map,long long int >; 和一个交互器 - map, long long int >::iterator it1;
我想知道 unsigned long long 和 unsigned long long int 的主要区别。它们可以互换使用吗? 对于像 9223372036854775807 这样的大十进制数的计
我看到的大多数代码都使用缩写类型来声明变量,例如 long long x; // long long int x short y; // short int y 我浏览了 C++11 标准(第 3.9
common_type::type是 unsigned long因为关于积分提升后的操作数,标准说... [...] if the operand that has unsigned integer
long long int A = 3289168178315264; long long int B = 1470960727228416; double D = sqrt(5); long lon
这些新数据类型的目的是什么?我通常只使用“int”或“long”,但为什么会存在这些呢?它们带来了什么新功能或用途? 最佳答案 long int一直是long的全称,只是很少用而已。 long lon
我正在运行以下for循环 for(unsigned long long int i = N-1; i >= 0; i--){ cin>>L[i]; } 当程序到达这个代码段时,它停止响应。但是
最近问了一个关于递归导致这个问题的问题 注意-> count() 函数返回键 K 在 map 容器中出现的次数。如果键存在于容器中,则返回 1,因为映射仅包含唯一键。如果 map 容器中不存在键,则返
好的,所以我正在尝试实现客户端 - 服务器程序(套接字编程)。 我的客户发送一个嵌入字符串中的 long long int,如下所示: char copy[10]; sprintf(send_data
如果我有任务 Long c = a + b; 有没有一种简单的方法来检查 a + b 不大于/小于 Long.MAX_VALUE/Long.MIN_VALUE? 最佳答案 使用 Guava , 就这么
我需要制作一个 Comparator 来根据它的 long 类型的变量之一对我的对象列表进行排序。 public class ParticipantIndexComparator implements
假设我有这两种类型: typedef unsigned long long uint64; typedef signed long long sint64; 我有这些变量: uint64 a = ..
long long 和 long 有什么区别?而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么? #include using namespace std; int
当结果将大于 C 中的 long long int 时,是否有可能对两个不同的 long long int 变量求和? 最佳答案 由于 OP 想要“在屏幕上打印结果”,因此将数字分成两部分:Most-
实际上我必须找到从源顶点到所有其他顶点的最短路径。为此,我获得了下面给出的代码模板。我想实现“Bellman–Ford algorithm”。 #include #include #include
我是一名优秀的程序员,十分优秀!