【发布时间】:2019-06-01 01:09:35
【问题描述】:
我正在尝试使用 Select crate 抓取网页:
let document = Document::from_read(response).unwrap();
for node in document.find(Class("lia-list-row")) {
let title = node.find(Class("page-link")).next().unwrap();
let title_text = title.text().trim();
println!("{}\n", title_text);
}
这会导致以下错误:
let title_text = title.text().trim();
^^^^^^^^^^^^ - temporary value is freed at the end of this statement
|
creates a temporary which is freed while still in use
println!("{} - {}\n", i, title_text);
---------- borrow used here, in later iteration of loop
我通过分离 .text() 和 .trim() 解决了这个问题
let title_text = title.text();
let trim_text = title_text.trim();
有什么区别?为什么第一次尝试失败了?
【问题讨论】:
-
请查看如何创建minimal reproducible example,然后查看edit 您的问题以包含它。我们无法分辨代码中存在哪些 crate、类型、特征、字段等。尝试在Rust Playground 上重现您的错误,或者您可以在全新的 Cargo 项目中重现它。还有Rust-specific MCVE tips。
标签: rust borrow-checker