#Brute forcing errors with loops angering the borrow checker

19 messages · Page 1 of 1 (latest)

narrow blade
#

I'm trying to brute force a web request to work with loops, however when I try to nest it with another brute force it gives me a borrow checker error says that it get's moved (val.text() is fn text(self), so it takes ownership). I get why it takes ownership, I just don't get why it matters. I'm not using val after I move it, so I'm confused.

Here is a simplified version of what I'm trying to do (also note that I'm writing this on the fly, so it may not be 100% semantically correct)

        fn foo() -> String {
          loop {
            match server.send_request() {
                Ok(val) => loop {
                    match val.text() {
                        Ok(val) => return Ok(val),
                        Err(err) => println!("Warning: failed to get query text; Error: {}", err)
                    }
                },
                Err(err) => println!("Warning: failed to query data; Error: {}", err)
            }
        }
twin scarab
#

If you can provide some actual errors, that would be helpful

#

-errors

terse epochBOT
#

If you're getting large or confusing errors please post the full error message from cargo check in a code block instead of the errors in your IDE so that we can understand your problem better:

```rust
// error from cargo check here
```

narrow blade
#
error[E0382]: use of moved value: `val`
   --> src/main.rs:32:27
    |
31  |                 Ok(val) => loop {
    |                    ---     ---- inside of this loop
    |                    |
    |                    move occurs because `val` has type `reqwest::blocking::Response`, which does not implement the `Copy` trait
32  |                     match val.text() {
    |                           ^^^ ------ `val` moved due to this method call, in previous iteration of loop
    |
note: `reqwest::blocking::Response::text` takes ownership of the receiver `self`, which moves `val`
   --> /home/usr/.cargo/registry/src/index.crates.io-6f17d22bba15001f/reqwest-0.11.23/src/blocking/response.rs:282:17
    |
282 |     pub fn text(self) -> crate::Result<String> {
    |                 ^^^^

For more information about this error, try `rustc --explain E0382`.
error: could not compile `webscraper` (bin "webscraper") due to previous error```
twin scarab
#

val moved due to this method call, in previous iteration of loop

#

and there you have it

#

though I'm not sure why you have the inner loop there

narrow blade
#

I need to keep running val.text() until it returns Ok()

twin scarab
#

you can't call val.text() twice, since it consumes val as you noted yourself

#

the outer loop, which repeatedly sends requests, makes sense to me

#

the inner doesn't (and causes the error)

narrow blade
#

oh it's because val.text() gets dropped. I was thinking that it moves it into the loop context, and only gets dropped after the loop finishes

#

I didn't relize it was dropped after one iteration

twin scarab
#

dropping has nothing to do with it

narrow blade
#

moving?

twin scarab
#

yes

#

your code basically does

match val.text() { ... }
match val.text() { ... }
match val.text() { ... }
...

after the first call to text, val has been moved into that function and you no longer own it

#

besides that, val.text() is most likely pure, and will not give you different results by calling it multiple times (ignore the fact that this is impossible in the first place)