I can't seem to copy a string including the heap. Clearly there is some concept I don't understand, I'm still a beginner in the particular language and still learning.
I'm getting a value returned into my variable called response
which has the datatype reqwest::Response
I want to copy the value of the string from here, so that I can work with that string and not worry about the variable called response
because I might be populating that with something else later on, I don't know the rest of my plan.
I've read how when copying a string in Rust is copying the pointers to a heap, but I want to copy the heap as well. This might not be the most optimal in terms of clock cycles of the CPU, but the bottle neck in my code is going to be the speed of the API calls across the internet, so waiting for a few clock cycles of the CPU to copy the heap is not a problem.
#[tokio::main]
pub async fn do_api_stuff7() -> Result<(), Box<dyn Error>> {
let response: reqwest::Response = reqwest::Client::new()
.get("https://www.themuse.com/api/public/jobs?page=1")
.header(AUTHORIZATION, "Bearer [AUTH_TOKEN]")
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json")
.send()
.await
.unwrap();
println!("one");
if response.status() != reqwest::StatusCode::OK {
println!("not OK");
} else {
println!("OK");
}
match response.status() {
reqwest::StatusCode::OK => {
println!("one");
println!("response {:?}", response);
println!("two");
let mut res_str: &str = "";
let mut str_is_ok = true;
match response.text_with_charset("utf-8").await {
Ok(res) => {
str_is_ok = true;
res_str = res.clone();
},
Err(_) => {
str_is_ok = false;
},
};
println!("three");
println!("{}", res_str);
println!("four");
}
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
}
other => {
panic!("Uh oh! Something unexpected happened: {:?}", other);
}
};
Ok(())
}
In my code if you have a look down to the second match
statement match response.text_with_charset("utf-8").await
I create a variable called res_str
just before the match statement so that the scope of this variable is outside that match
statement.
Inside this second match statement I try to copy
or clone
or something anything just to copy the string including the heap.
Then after this second match statement have println!("{}", res_str);
so I can print out the string.
I don't know if this second match statement was the right thing to do but I've been trying all sorts of things.
How do I copy a string including the heap?