-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't compile the http server example #1765
Comments
Hi @dotSlashLu example is working for me after couple of changes mentioned as a comment in the following code: use tokio::net::TcpListener;
use tokio::prelude::*;
use std::net::SocketAddr; // Added `SocketAddr` in scope
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr: SocketAddr = "127.0.0.1:8080".parse()?; // explicitly specified `SocketAddr`
let mut listener = TcpListener::bind(&addr).await.unwrap(); // unwrap after await
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0u8; 1024]; // explicitly specified type `u8`
// In a loop, read data from the socket and write the data back.
loop {
let n = match socket.read(&mut buf).await {
// socket closed
Ok(n) if n == 0 => return,
Ok(n) => n,
Err(e) => {
println!("failed to read from socket; err = {:?}", e);
return;
}
};
// Write the data back
if let Err(e) = socket.write_all(&buf[0..n]).await {
println!("failed to write to socket; err = {:?}", e);
return;
}
}
});
}
} |
Thanks @abdul-rehman0 , the unwrap error makes sense to me now. Seems the example needs to be corrected then. |
@dotSlashLu if you check out the main page of this repository, you will find the correct working example. The documentation on the crate seems to be outdated. |
The docs found on |
Version
rustc 1.39.0 (4560ea788 2019-11-04)
Platform
Linux jarvis.local 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Description
I tried to compile the http server example on the documentation index, but I can't get it compiled with type annotation error and mainly this
no method named unwrap found for type impl std::future::Future in the current scope
error I don't know how to solve since this is my first try to update to tokio-0.2.x and std future.The text was updated successfully, but these errors were encountered: