Enum type print string

enum qmessage{
    Move{ x : i32, y : i32},
    User(String),
}
    
impl qmessage{
    fn printf(&self){
        println!("str : {} " , self); // but Error
    }
}
    
fn main(){
    let vprint = qmessage::User(String::from("== hello ==="));
    vprint.printf();
}

how to print or how to cast (self to String)

I found Here

impl qmessage{
    fn printf(&self){
        
        match self {
            qmessage::User(v) =>{println!("{}", v)},
            _ => println!("c====")
        }
    }
}

If you know about Traits already, there's the "Debug" and "Display" traits. They allow you to print your enums/structs.
Sorry, no time to elaborate right now, look it up or wait a little. I'm sure someone else will reply soon.

1 Like

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.