Skip to main content
1 of 3

There already is a generic fallible conversion implementated from &HashMap<_, _> to HeaderMap: https://docs.rs/http/latest/src/http/header/map.rs.html#2025

Hence, your entire function is superfluous:

use http::HeaderMap;
use std::collections::HashMap;

fn main() {
    let mut hash_map = HashMap::new();
    hash_map.insert("Content-Type".to_string(), "application/json".to_string());
    let header_map: HeaderMap = (&hash_map)
        .try_into()
        .expect("Hash map should contain valid headers.");
    dbg!(header_map);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=63e36c4049adf44ccf1adf6f807bf451