I did the hello world not so long ago and I'm not making much progress since then. Can someone explain what I'm doing wrong please, because it doesn't matter how many help documents I read this doesn't make any sense.
I've created a variable called response
and I'm trying to use this a few different times and it's all going wrong.
I've read tutorials where if you assign a variable to another variable then the first variable is dis-guarded, but I'm not assign a variable to another variable. However I'm still having problems.
#[tokio::main]
pub async fn do_api_stuff4() -> 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");
println!("response.error_for_status() {:?}", response.error_for_status());
println!("two");
if response.status() != reqwest::StatusCode::OK {
println!("not OK");
} else {
println!("OK");
}
Ok(())
}
In the line println!("response.error_for_status() {:?}", response.error_for_status());
I use the variable response
and then after this I'm not able to use the variable again.
So on compile time the line if response.status() != reqwest::StatusCode::OK {
doesn't compile.
I haven't made a copy of the variable, I've just used it.
This is what I get when I compile it.
Compiling prj_the_muse_api v0.1.0 (/home/matthew/Rust/009 call API/prj_the_muse_api)
error[E0382]: borrow of moved value: `response`
--> src/mod_api_request_calls.rs:211:8
|
183 | let response: reqwest::Response = reqwest::Client::new()
| -------- move occurs because `response` has type `Response`, which does not implement the `Copy` trait
...
203 | println!("response.error_for_status() {:?}", response.error_for_status());
| ------------------ `response` moved due to this method call
...
211 | if response.status() != reqwest::StatusCode::OK {
| ^^^^^^^^^^^^^^^^^ value borrowed here after move
|
note: `Response::error_for_status` takes ownership of the receiver `self`, which moves `response`
--> /home/matthew/.cargo/registry/src/github.com-1ecc6299db9ec823/reqwest-0.11.16/src/async_impl/response.rs:341:29
|
341 | pub fn error_for_status(self) -> crate::Result<Self> {
| ^^^^
For more information about this error, try `rustc --explain E0382`.
error: could not compile `prj_the_muse_api` due to previous error
I don't want a move to occur, I want to be able to use the same variable more than once.
When I look up help documents about moves and copies, it always refers to assigning another variable invoking to move. Which is not what I'm doing, println!("response.error_for_status() {:?}", response.error_for_status());
does not assign the variable response
to another variable.
I'm confused.