Skip to content

How to use addr #14

Answered by EstebanBorai
xhyrom asked this question in Q&A
Jul 30, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Hi @xhyrom! Thanks for opening this question.

As I understand, you want to have the Network Interface's name and IP to build a your NetworkInfo instance, I assume the type for address to be an instance of std::net::IpAddr. What do you think about this approach?

use network_interface::{Addr, NetworkInterface, NetworkInterfaceConfig};
use std::net::IpAddr;

struct NetworkInfo {
    name: String,
    addr: IpAddr,
}

fn main() {
    let network_interfaces = NetworkInterface::show().unwrap();
    let ifaces = network_interfaces.iter().filter_map(|netifa| {
        if let Some(netifa_addr) = netifa.addr {
            let addr: IpAddr = match netifa_addr {
                Addr::V4(ip_addr) => I…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@xhyrom
Comment options

Answer selected by xhyrom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants