'the trait `Future` is not implemented for `Result<std::net::TcpListener, std::io::Error>
I try to initialize a server in rust with async functionality using Tokio library:
use std::net::TcpListener;
#[tokio::main]
async fn main() {
// let listener=TcpListener::bind("localhost:8080").await.unwrap();
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
let (socket,_addr)=listener.accept().await.unwrap();
}
This throws an error saying that:
the trait `Future` is not implemented for `Result<std::net::TcpListener, std::io::Error>`
When I hover over listener its type is unknown.
Solution 1:[1]
You have:
use std::net::TcpListener;
And you probably want to have:
use tokio::net::TcpListener;
Here is an example from tokio's docs:
use tokio::net::TcpListener;
use std::io;
async fn process_socket<T>(socket: T) {
// do work with socket here
}
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (socket, _) = listener.accept().await?;
process_socket(socket).await;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
