0
votes

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).

1

1 Answers

2
votes

You're depending on tokio v1.11.0 (the current release), but your docs are for v0.1.22. The interface has changed quite drastically, that's why you are not finding all those types, functions and modules. Current documentation is here.

If you found the documentation via Google: The problem of Google returning old docs.rs results is well known. There is an open issue that will solve this problem.

Also, docs.rs displays a warning on the top left of the page to go to the latest version if you are not on that version anyway. You should get in the habit of clicking it unless you know you're using an older version. (Thanks to kmdreko for pointing this out in the comments)