I am trying to use tokios background event loop feature, but I am unable to use any tokio reactor features and I have no idea why. I created a new project with cargo new tokio-test --bin
and copy-pasted the code snippet and in the link for establishing a basic TCP connection.
Cargo.toml
[package]
name = "tokio-test"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.11.0", features = ["full"] }
main.rs
use tokio::prelude::*;
use tokio::net::TcpStream;
fn main() {
let addr = "93.184.216.34:9243".parse().unwrap();
let connect_future = TcpStream::connect(&addr);
let task = connect_future
.and_then(|socket| {
println!("successfully connected");
Ok(())
})
.map_err(|e| println!("failed to connect; err={:?}", e));
tokio::run(task);
}
When running cargo build
I get:
error[E0432]: unresolved import `tokio::prelude`
--> src/main.rs:1:12
|
1 | use tokio::prelude::*;
| ^^^^^^^ could not find `prelude` in `tokio`
error[E0425]: cannot find function `run` in crate `tokio`
--> src/main.rs:16:12
|
16 | tokio::run(task);
| ^^^ not found in `tokio`
error[E0599]: no method named `and_then` found for opaque type `impl Future` in the current scope
--> src/main.rs:10:10
|
10 | .and_then(|socket| {
| ^^^^^^^^ method not found in `impl Future`
|
help: consider `await`ing on the `Future` and calling the method on its `Output`
|
10 | .await.and_then(|socket| {
| ^^^^^^
Clearly I am missing some directive from tokio
but I cannot figure how to include it. My rustc version is rustc 1.55.0 (c8dfcfe04 2021-09-06)
.