我正在使用关于他们的新板条箱的弹性搜索博客文章中的示例代码,但我无法让它按预期工作。线程恐慌thread 'main' panicked at 'not currently running on the Tokio runtime.'。
什么是 Tokio 运行时,如何配置它,为什么必须配置?
use futures::executor::block_on;
async elastic_search_example() -> Result<(), Box<dyn Error>> {
let index_response = client
.index(IndexParts::IndexId("tweets", "1"))
.body(json!({
"user": "kimchy",
"post_date": "2009-11-15T00:00:00Z",
"message": "Trying out Elasticsearch, so far so good?"
}))
.refresh(Refresh::WaitFor)
.send()
.await?;
if !index_response.status_code().is_success() {
panic!("indexing document failed")
}
let index_response = client
.index(IndexParts::IndexId("tweets", "2"))
.body(json!({
"user": "forloop",
"post_date": "2020-01-08T00:00:00Z",
"message": "Indexing with the rust client, yeah!"
}))
.refresh(Refresh::WaitFor)
.send()
.await?;
if !index_response.status_code().is_success() { …Run Code Online (Sandbox Code Playgroud) 我在使用Iterator'sflat_map函数时遇到了困难,我不太确定如何理解和解决这个编译器错误。
我通过序列化两个结构将文件路径列表 flat_mapping 成两个字符串:
let body: Vec<String> = read_dir(query.to_string())
.iter()
.enumerate()
.flat_map(|(i, path)| {
let mut body: Vec<String> = Vec::with_capacity(2);
let entry = Entry { i };
body.push(serde_json::to_string(&entry).unwrap());
let record = parse_into_record(path.to_string()).unwrap();
body.push(serde_json::to_string(&record).unwrap());
body.iter()
})
.collect();
Run Code Online (Sandbox Code Playgroud)
error[E0277]: a value of type `std::vec::Vec<std::string::String>` cannot be built from an iterator over elements of type `&std::string::String`
--> src/main.rs:275:10
|
275 | .collect();
| ^^^^^^^ value of type `std::vec::Vec<std::string::String>` cannot be built from `std::iter::Iterator<Item=&std::string::String>`
|
= help: the trait `std::iter::FromIterator<&std::string::String>` …Run Code Online (Sandbox Code Playgroud) 我正在抓取的网站要求我查询 HTML 页面的标题标签以及一些其他元素,看看我是否可以辨别文章的标题。
我创建一个HashMap<&str, u8>并立即.insert(title_tag_text, 1)查询标题元素,然后我希望将标题标签的文本类似地插入到哈希映射中,但我收到错误borrowed value does not live long enough。
我不确定我是否理解,因为我认为我正确地取消引用了应该实现该特征的std::string::Stringa ?不幸的是,我怀疑我计划实现的下一个代码也有类似的问题。&strCopy
let mut title_candidates: HashMap<&str, u8> = HashMap::new();
let title_tag_text: String = Selector::parse("title")
.ok()
.and_then(|selector| html_document.select(&selector).next())
.map(|elem| elem.inner_html())?;
title_candidates.insert(&*title_tag_text, 1);
Selector::parse("h1, h2, h3, .title")
.ok()
.as_ref()
.map(|selector| html_document.select(selector))?
.map(|elem| elem.inner_html()) // std::string::String
.for_each(|title| {
*title_candidates.entry(&*title).or_insert(0) += 1;
// if title_tag_text.contains(&*title.as_str()) {
// *title_candidates.entry(&*title_tag_text) += 1;
// }
});
Run Code Online (Sandbox Code Playgroud)
error[E0597]: `title` does not live long enough
--> src/main.rs:140:39 …Run Code Online (Sandbox Code Playgroud)