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







