In Rust, a future is a type that represents an asynchronous operation that may not have completed yet. Futures are for writing non-blocking code, such as to read a file, make a web request, or query a database.
Rust's futures are composable, which means that multiple futures can be combined to create more complex workflows. Futures can be chained together to form a pipeline, with each future as a step in the pipeline. When a future completes, it can trigger the next future to execute.
Futures are executed by an executor, which is responsible for scheduling and running the futures. Rust provides several built-in executors.
Example of a Rust future for an asynchronous HTTP request:
use futures::Future;
use reqwest::Url;
async fn fetch_url(url: Url) -> Result<String, reqwest::Error> {
let response = reqwest::get(url).await?;
let text = response.text().await?;
Ok(text)
}
fn main() {
let url = Url::parse("https://example.com").unwrap();
let future = fetch_url(url);
let runtime = tokio::runtime::Runtime::new().unwrap();
let text = runtime.block_on(future).unwrap();
println!("response text is {}", text)
}
This example defines an asynchronous function fetch_url
. The function accepts a URL, then uses the reqwest
crate to make an HTTP GET request to the URL, then returns the response text as a String
.
The fetch_url
function is async, so returns a Future
that we store in a variable. We use the tokio runtime to run the Future
. This blocks until it completes. Finally, we print the result.