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.
If you are seeing errors, it's often a good idea to paste the compiler output so we can see the error message too
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:
[package]
name = "repro"
version = "0.1.0"
edition = "2021"
[dependencies]
if-addrs = "0.6.7"
When referring to the crate in your main.rs 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);
}
}
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.