How to Handle DNS Resolution in Rust

Web
Use external crates like hickory-resolver to handle DNS resolution in Rust since the standard library lacks built-in support.

Rust does not have a built-in DNS resolver in the standard library; you must use an external crate like hickory-resolver or trust-dns-resolver to perform DNS lookups. Add the crate to your Cargo.toml and use its Resolver to query hostnames asynchronously or synchronously.

use hickory_resolver::TokioAsyncResolver;
use hickory_resolver::config::{ResolverConfig, ResolverOpts};

#[tokio::main]
async fn main() {
    let config = ResolverConfig::default();
    let opts = ResolverOpts::default();
    let resolver = TokioAsyncResolver::tokio(config, opts);
    let lookup = resolver.lookup_ip("www.rust-lang.org").await.unwrap();
    println!("IP: {:?}", lookup.iter().next().unwrap());
}