Hello !
I'm trying to transform a lines() with string of 2 word separated by space to a hashmap and I'm a little stuck.
I'm trying something like that:
let results: HashMap<String,String> =reqwest::get(toFetch.as_str())
.expect("Error in GET request")
.text()
.expect("Error parsing response")
.lines().map(|line|line.split_whitespace().collect()).collect();
But I have no idea how to make it a Hashmap, I only managed to make a vector of strings like that :
let results: Vec<String> = reqwest::get(url.as_str())
.expect("Error in GET request")
.text()
.expect("Error parsing response")
.lines()
.map(|item| item.to_string())
.collect()
Could you help me make it a HashMap<String,String> or even a Vec!<(String,String)> (Vector of tuples) ?
Thank you
Hey Thank you for your help.
I'm just having an issue to adapt it to my case, here is my function :
fn get_archives(url: &str) -> HashMap<String,String>{
let toFetch = format!("https://web.archive.org/cdx/search/cdx/?url={}&output=text&fl=timestamp,original&filter=statuscode:200&collapse=digest", url);
let lines = reqwest::get(toFetch.as_str())
.expect("Error in GET request")
.text()
.expect("Error parsing response")
.lines();
let mut data = HashMap::new();
for line in lines {
match line.split_whitespace().collect::<Vec<&str>>().as_slice() {
[s1, s2] => {data.insert(s1.to_string(), s2.to_string());}
_ => {panic!("Invalid Value for archive. line : {}", line);}
}
}
data
}
And I have the following issue :
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:142:17
|
142 | let lines = reqwest::get(toFetch.as_str())
| _________________^
143 | | .expect("Error in GET request")
144 | | .text()
145 | | .expect("Error parsing response")
| |_________________________________________^ creates a temporary which is freed while still in use
146 | .lines();
| - temporary value is freed at the end of this statement
147 | let mut data = HashMap::new();
148 | for line in lines {
| ----- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
I don't understand why when I clone it or use a ref the error is still there...
fn get_archives(url: &str) -> HashMap<String,String>{
let toFetch = format!("https://web.archive.org/cdx/search/cdx/?url={}&output=text&fl=timestamp,original&filter=statuscode:200&collapse=digest", url);
let mut lines : Vec<String >= reqwest::get(toFetch.as_str())
.expect("Error in GET request")
.text()
.expect("Error parsing response")
.lines().map(|x|x.to_owned()).collect();
let mut data = HashMap::new();
for line in lines {
match line.split_whitespace().collect::<Vec<&str>>().as_slice() {
[s1, s2] => {data.insert(s1.to_string(), s2.to_string());}
_ => {panic!("Invalid Value for archive. line : {}", line);}
}
}
data
}