#Borrowing Problem

4 messages · Page 1 of 1 (latest)

frank tapir
#

And this is my error

#
error[E0597]: `inp` does not live long enough
  --> src/main.rs:11:14
   |
11 |       let sp = inp.split(" ");
   |                ^^^^^^^^^^^^^^ borrowed value does not live long enough
...
28 |       let scan = thread::spawn( move || for port in ranges..=rangef/2{
   |  ________________-
29 | |             let addr = SocketAddr::from_str(format!("{}:{:?}", ip , port ).as_str()).unwrap();
30 | |             match TcpStream::connect_timeout(&addr,Duration::from_secs(3)){
31 | |                 Ok(_) => println!("{} Okay", addr ),
32 | |                 Err(_) => println!("{} Not Okay", addr),
33 | |             }
34 | |         });
   | |__________- argument requires that `inp` is borrowed for `'static`
...
45 |   }
   |   - `inp` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
warning: `Port_Scanner` (bin "Port_Scanner") generated 1 warning
error: could not compile `Port_Scanner` due to previous error; 1 warning emitted

#

I think no mistake in code

dry grail
#

Are you using imp (or sp) inside the closure?. thread::spawn requires the closure live for 'static but inp is getting dropped before that point.

You can use thread::scope for scoped threads which can borrow shorter lived data because they guarantee all threads are joined when the scope function is exited