Last active
December 2, 2019 00:31
-
-
Save boxofrox/73583f1c2139483a37d82af716c84371 to your computer and use it in GitHub Desktop.
rustc E0277 when using async block vs without.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`) | |
--> src/main.rs:13:21 | |
| | |
13 | let _conn = tokio::net::TcpListener::bind(&addr).await.map_err(|_err| "help")?; | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()` | |
| | |
= help: the trait `std::ops::Try` is not implemented for `()` | |
= note: required by `std::ops::Try::from_error` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "debug-await-method-chaining" | |
version = "0.1.0" | |
authors = ["boxofrox"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
tokio = { version = "0.2", features = [ "full" ] } | |
futures = "0.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::error::Error; | |
#[tokio::main] | |
async fn main() { | |
fails().await.unwrap(); | |
works().await.unwrap(); | |
} | |
async fn fails() -> Result<(), Box<dyn Error>> { | |
let addr = "127.0.0.1:8888".parse::<std::net::SocketAddr>()?; | |
let server = async { | |
let _conn = tokio::net::TcpListener::bind(&addr).await.map_err(|_err| "help")?; | |
}; | |
server.await; | |
Ok(()) | |
} | |
async fn works() -> Result<(), Box<dyn Error>> { | |
let addr = "127.0.0.1:8888".parse::<std::net::SocketAddr>()?; | |
let _conn = tokio::net::TcpListener::bind(&addr).await.map_err(|_err| "help")?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment