Skip to content

Instantly share code, notes, and snippets.

@boxofrox
Last active December 2, 2019 00:31
Show Gist options
  • Save boxofrox/73583f1c2139483a37d82af716c84371 to your computer and use it in GitHub Desktop.
Save boxofrox/73583f1c2139483a37d82af716c84371 to your computer and use it in GitHub Desktop.
rustc E0277 when using async block vs without.
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`
[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"
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