简体   繁体   中英

Error while instantiating an enum in rust

I am trying to do this:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

// A network
pub enum IpNetwork {
    V4(Ipv4Network),
    V6(Ipv6Network),
}

pub struct Ipv4Network {
    addr: Ipv4Addr,
    prefix: u8,
}

pub struct Ipv6Network {
    addr: Ipv6Addr,
    prefix: u8,
}

impl Ipv4Network {
    fn new(addr: Ipv4Addr, prefix: u8) -> Ipv4Network {
        Ipv4Network { addr:addr, prefix:prefix }
    }
}

impl Ipv6Network {
    fn new(addr: Ipv6Addr, prefix: u8) -> Ipv6Network {
        Ipv6Network { addr:addr, prefix:prefix }
    }
}

impl IpNetwork {
    pub fn new(ip: IpAddr, prefix: u8) -> IpNetwork {
        match ip {
            IpAddr::V4(a) => IpNetwork::V4(a, prefix),
            IpAddr::V6(a) => IpNetwork::V6(a, prefix),
        }
    }
}

fn main() {
    let ip = Ipv4Addr::new(77, 88, 21, 11);
    let cidr = IpNetwork::new(ip, 24);
}

And this gives me:

src/lib.rs:34:30: 34:54 error: this function takes 1 parameter but 2 parameters were supplied [E0061]
src/lib.rs:34             IpAddr::V4(a) => IpNetwork::V4(a, prefix),
                                           ^~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:35:30: 35:54 error: this function takes 1 parameter but 2 parameters were supplied [E0061]
src/lib.rs:35             IpAddr::V6(a) => IpNetwork::V6(a, prefix),
                                           ^~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:42:31: 42:33 error: mismatched types:
 expected `std::net::ip::IpAddr`,
    found `std::net::ip::Ipv4Addr`
(expected enum `std::net::ip::IpAddr`,
    found struct `std::net::ip::Ipv4Addr`) [E0308]
src/lib.rs:42     let cidr = IpNetwork::new(ip, 24);
                                            ^~
error: aborting due to 3 previous errors

Why does rust think the constructor takes one argument?

变体的形式为V4(Ipv4Network) ,因此您应该传递一个Ipv4Network例如Ipv4Network::new(a, prefix)

IpAddr::V4(a) => IpNetwork::V4(Ipv4Network::new(a, prefix)),

There are two errors in your code:

First error is that you eg construct IpNetwork::V4 by passing a tuple of address and a prefix. If you look at how you defined IpNetwork :

// A network
pub enum IpNetwork {
   V4(Ipv4Network),
   V6(Ipv6Network),
}

You need to supply IpNetwork::V4 with struct Ipv4Network and not just the tuple of (a, prefix) . Same goes for IpNetwork::V6 . With these adjustment your match brace becomes:

pub fn new(ip: IpAddr, prefix: u8) -> IpNetwork {
    match ip {
        IpAddr::V4(a) => IpNetwork::V4(Ipv4Network::new(a, prefix)),
        IpAddr::V6(a) => IpNetwork::V6(Ipv6Network::new(a, prefix)),
    }
}

Second error is in main method. You are constructing Ipv4Addr and trying to pass it to IpNetwork as a parameter. While IpNetwork only accepts IpAddr . So you'll missing this part:

let addr = IpAddr::V4(ip);
let cidr = IpNetwork::new(addr, 24);

Here is playpen link to solution that fails on unstable parameters.

For this snippet to work you'll need to make a crate and add #![feature(ip_addr)] for it to pass compiler checks. IpAddr and variants are apparently being reworked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM