Rust crates with hyphens - can't use

I am trying to use this crate:

but when I try to add it to my cargo.toml file I get errors and can't have a use or extern crate statement in either format in my code. Rust analyzer gives me unresolved extern crate or unresolved import.

How can I use this crate?

If you are seeing errors, it's often a good idea to paste the compiler output so we can see the error message too :wink:

If you want to use a 3rd party crate with hyphens in the name, first you add its name to your Cargo.toml file, leaving the hyphens as-is:

name = "repro"
version = "0.1.0"
edition = "2021"

if-addrs = "0.6.7"

When referring to the crate in your you need to use if_addrs.

The compiler automatically replaces -'s with _ because if-addrs would be parsed as the if keyword subtracted by some addrs value, which isn't a valid identifier in Rust.

fn main() {
    for iface in if_addrs::get_if_addrs().unwrap() {
        println!("{:#?}", iface);
1 Like

Thank you for the suggestion to paste the compiler error. Apparently there was a conflict with a previously added, but currently unused package. Rust-Analyzer was giving a bad message, but the compiler gave a MUCH better description of the problem that now allows compilation with no errors.

The error wasn't with the hyphens after all.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.