Skip to content

Rust does not show a compiler warning if you don't use a future #142196

Closed as not planned
@iyernaveenr

Description

@iyernaveenr

Quoting from Rustbook - 17.1. Futures and the Async Syntax - Defining the page_title Function:

In fact, Rust will show a compiler warning if you don’t use a future.

This is not accurate if that async block is assigned to a variable. Here's some code to replicate that:

Code

extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
    trpl::run(async {
        let fut1 = async {
            for i in 1..10 {
                println!("hi number {i} from the first task!");
                trpl::sleep(Duration::from_millis(500)).await;
            }
        };
        fut1.await;
        let _ = async {
            for i in 1..5 {
                println!("hi number {i} from the second task!");
                trpl::sleep(Duration::from_millis(500)).await;
            }
        };
    });
}

Meta

cargo --version

cargo 1.87.0 (99624be96 2025-05-06)

cargo rustc -- --version --verbose

   Compiling hello-async v0.1.0 (/home/user/hello-async)
rustc 1.87.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.87.0-dev
LLVM version: 20.1.1
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

rustc --version --verbose:

rustc 1.87.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.87.0-dev
LLVM version: 20.1.1

Error output

cargo build

   Compiling hello-async v0.1.0 (/media/nibu/ext4_data/ext4_progprac/Basics/hello-async)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.47s

cargo run

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/hello-async`
hi number 1 from the first task!
hi number 2 from the first task!
hi number 3 from the first task!
hi number 4 from the first task!
hi number 5 from the first task!
hi number 6 from the first task!
hi number 7 from the first task!
hi number 8 from the first task!
hi number 9 from the first task!

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions