- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法让这个向量的切片持续足够长的时间,以便我可以在这种循环结构中使用它们?
fn populate_chain(file_path: &str) -> HashMap<String, HashSet<&String>> {
println!("loading...");
let time = util::StopWatch::new();
let mut words = HashMap::new();
{
let f = |mut x: Vec<String>| {
let word = x.pop().unwrap();
words.insert(word, HashSet::new());
};
Csv::process_rows(f, file_path, "\t");
}
let col: Vec<(String, HashSet<&String>)> = words.clone().into_iter().collect();
let m: usize = col.len() - 1;
for i in 0..m {
let ref k: String = col[i].0;
for j in i..m {
let ref nk: String = col[j].0;
if check_link(k, nk) {
words.get_mut(k).unwrap().insert(nk);
words.get_mut(nk).unwrap().insert(k);
}
}
}
time.print_time();
words
}
我使用双 for 循环将相关的单词链接在一起,以便以后可以快速查找它们。
这是编译器错误...
error: `col` does not live long enough
--> src/main.rs:28:29
|
28 | let ref k: String = col[i].0;
| ^^^ does not live long enough
...
40 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the block at 13:72...
--> src/main.rs:13:73
|
13 | fn populate_chain(file_path: &str) -> HashMap<String, HashSet<& String>>{
| ^
error: `col` does not live long enough
--> src/main.rs:30:34
|
30 | let ref nk: String = col[j].0;
| ^^^ does not live long enough
...
40 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the block at 13:72...
--> src/main.rs:13:73
|
13 | fn populate_chain(file_path: &str) -> HashMap<String, HashSet<& String>>{
|
最佳答案
我可以从您的函数签名中告诉您,您在尝试编写此函数时会遇到大问题,至少如果您希望它包含非空 HashSet
的话。 s 在结果中。
fn populate_chain(file_path: &str) -> HashMap<String, HashSet<&String>>
这个函数签名里面有引用;他们已经消逝了一生。如果您将推断的生命周期明确化,它将如下所示:
fn populate_chain<'a>(file_path: &'a str) -> HashMap<String, HashSet<&'a String>>
换句话说:这个函数声称,给定一些生命周期为 'a
的字符串切片,它将返回一个包含 String
的集合具有生命周期的对象 'a
.
但是你没有办法分配这样的 String
代码中的对象。 :(
所以,你被困住了;无论您在该函数体中放入什么,您都无法提供返回关于 HashSet
的重要结果的实现。
然而,一切并没有丢失。例如,您可以修改您的函数,以便它也将对 TypedArena
的引用作为附加参数。具有适当的生命周期,然后在那里分配字符串。另一个(更简单的)选项是使用 HashSet<String>
而不是 HashSet<&String>
...
关于vector - 需要矢量切片才能活得更久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600519/
简而言之: 我怎样才能切片?也就是说,能够指定我想从多个索引(例如 y = x[(2, 5, 11)] )中提取,而不仅仅是单个索引(例如 y = x[2] )。 简单示例 : 说我有这个数据: d
是否可以在 F# 中对 Array2D 进行切片? 说,let tmp =Array2D.init 100 100 (fun x y -> x * 100 + y) 如何从 tmp 中检索某些列或某些
例如,我希望文本仅显示“此处”,但它不起作用。文本经常变化,但我需要的单词保持在固定位置。我想访问该词。 我做错了什么? function myFunction() { var x = doc
当尝试使用spring的分页或切片来迭代非常大的mongodb集合时,程序运行正常,但在某些时候下一页/切片为空,并且在调试时出现“包含未知实例的页面/切片”消息. 这是代码示例: do { Pa
有人能给我一个关于如何分割 ListView 的例子吗?我正在使用 SimpleCursorAdapter 在 ListView 中显示数据.. 我的代码是这样的。 private WordDbAda
这个问题在这里已经有了答案: C++ slicing causing leak / undefined behavior / crash (3 个答案) 关闭 8 年前。 如果我有如下代码: cla
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 5 年前。 我目前有 500 行数据。我想使用前五十行,然后跳过 50 行,依此类推。我该如何继续这
为什么对一行或一列进行切片会产生“无量纲数组”?例如: import numpy as np arr = np.zeros((10,10)) print arr.shape # (10, 10) 但是
我有以下 pandas 数据框: Shortcut_Dimension_4_Code Stage_Code 10225003 2 8225003
如何通过数组为 ruby 中的散列创建切片,如下所示: info = { :key1 => "Lorem", :key2 => "something...", :key3 => "
这个问题在这里已经有了答案: regex to get all text outside of brackets (4 个答案) 关闭 5 年前。 我正在编写的这个程序接收到一个大小不同的字符串,其
我尝试使用 tf.Tensor.getitem 对张量进行切片功能如下: indices = [0, 5] data[:,:,indices] 但是我得到以下错误: TypeError: can on
这个问题在这里已经有了答案: Can I create a "view" on a Python list? (10 个答案) 关闭 7 年前。 有没有一种方法可以在 Python 3 中创建序列的
我想弄清楚如何从多维数组中获取单个维度(为了论证,假设它是二维的),我有一个多维数组: double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4,
我有一个 std::vector。我想创建代表该 vector 切片的迭代器。我该怎么做?在伪 C++ 中: class InterestingType; void doSomething(slice
写在前面 前面的文章介绍了Go的一些基本类型,本文开始涉及Go的一些容器类型,它们都是可以包含多个元素的数据结构,如数组、切片、map 数组 数组是具有相同类型且长度固定的一组元素集合,定义的格式:v
给定一个 numpy 数组和一个 __getitem__ 类型的索引,是否有一种惯用的方法来获取数组的相应切片,总是返回一个数组而不是标量? 有效索引的示例包括:int、slice、省略号或上述的元组
我创建了一个继承自 pandas.DataFrame 的类。在此类中添加了元数据(不是添加到列中,而是添加到类实例中): class MeasurementPoint(pandas.DataFrame
我想在空间上剪切视频以生成 N x M 个文件。 例如,我想把 test.video 拆分成 NxM 的瓦片? Video tiles 最佳答案 您可以使用 ffmpeg 及其 crop filter
这是一个示例代码。比如我想拉德国 在页面加载时切片。在这段代码中,它拉取第一个切片。 无功图; var 传说; var chartData = [{ 国家:“立陶宛”, 值:260}, { 国家:“爱
我是一名优秀的程序员,十分优秀!