liveBook · Manning

Feature flag

As explained in Learn Rust in a Month of Lunches

A feature flag is a configuration option that conditionally enables code, dependencies, or capabilities during compilation.

A feature flag is a configuration option that conditionally enables part of a crate during compilation. Crates enable some code by default, while optional capabilities must be selected explicitly in Cargo.toml. Disabled code is effectively unavailable to the compiler. section 19.2

Feature flags help you:

  • Reduce compilation time by compiling only the crate functionality you use. section 19.0
  • Enable optional APIs such as reqwest's blocking client with cargo add reqwest --feature blocking. section 19.2
  • Diagnose missing types or modules whose implementation is hidden behind an unenabled flag. section 19.2 section 19.4

Enabling reqwest's blocking client

A default reqwest dependency may not provide reqwest::blocking::Client. Add the feature explicitly: reqwest = { version = "0.11.22", features = ["blocking"] }. The following code can then create a blocking client, send a request, read the response, and print its text: let client = reqwest::blocking::Client::default(); let response = client.get("https://www.rust-lang.org").send().unwrap(); println!("{}", response.text().unwrap()); section 19.2

      Adding reqwest v0.11.18 to dependencies.
             Features:
             + __tls
             + default-tls
             + hyper-tls
             + native-tls-crate
             + tokio-native-tls
             - __internal_proxy_sys_no_cache
             - __rustls
             - async-compression
             - blocking
             - brotli
             - cookie_crate
             - cookie_store
             - cookies
             - deflate
             - gzip
             - hyper-rustls
             - json
             - mime_guess
             - multipart
             - native-tls
             - native-tls-alpn
             - native-tls-vendored
             - proc-macro-hack
             - rustls
             - rustls-native-certs
             - rustls-pemfile
             - rustls-tls
             - rustls-tls-manual-roots
             - rustls-tls-native-roots
             - rustls-tls-webpki-roots
             - serde_json
             - socks
             - stream
             - tokio-rustls
             - tokio-socks
             - tokio-util
             - trust-dns
             - trust-dns-resolver
             - webpki-roots

Inspect the feature list to distinguish enabled features marked with + from disabled features marked with -. The listing shows blocking disabled by default. section 19.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains defaults, Cargo.toml syntax, compiler visibility, and source-level cfg gating, but does not develop flag implementation beyond these examples.
Examples
Uses reqwest's blocking client, Cargo feature listings, cargo add, Cargo.toml entries, and compiler errors for unavailable APIs.
What it assumes
Assumes basic Rust crate use and Cargo dependency configuration while explaining feature flags from the beginning.
What you can do after
Operate/configure Cargo dependencies by inspecting feature lists and enabling required crate functionality explicitly.
How it connects
Connects feature flags with Cargo.toml, crates, compilation, reqwest, blocking APIs, and async Rust.

Also explained in

Compare how 8 books explain feature flag →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.