0
votes

I am learning Rust and I am trying to write a simple IP handling function

enum IpAddr{
   v4(u8,u8,u8,u8),
   v6(String),
}

impl IpAddr{
    fn write(&self){
        match *self {
            IpAddr::v4(A,B,C,D) => println!("{}.{}.{}.{}",A,B,C,D),
            IpAddr::v6(S) => println!("{}",S) 
        }
    }
}

The v4 matches fine, but I get the following build error on the 2nd one

error[E0507]: cannot move out of self.0 which is behind a shared reference

move occurs because _S has type String, which does not implement the Copy trait

How can I match on a enum with a String attached?

1
FYI, you aren't following the Rust naming conventions (which should be generating a bunch of warnings, by default). - Herohtar

1 Answers

0
votes

Its complaining because you are trying to copy self by dereferencing, but IpAddr contains a String which is not copy-able.

Remove the dereference and it should work as expected

match self {
    IpAddr::v4(A,B,C,D) => println!("{}.{}.{}.{}",A,B,C,D),
    IpAddr::v6(S) => println!("{}",S) 
}