Rust lifetime

Home   »   Rust lifetime

// Similarly, both references here must outlive this structure.

use std::fmt;

//#[derive(Debug)]
struct What<'a> {
    x: &'a str,
    y: &'a str,
}

impl <'a> fmt::Display for What<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {}", self.x, self.y)
    }
}

impl <'a> What<'_> {
    fn curse(&self){
        println!("{} {} {}", self.x, "The fudge", self.y);
    }
}

fn main() {
    println!("Hello, world!");
    
    let what = What{x: "What", y: "Is this?"};
    
    println!("{}", what);
    what.curse();
}

Leave a Reply

Your email address will not be published.