Created
July 17, 2020 01:54
-
-
Save bojand/f0a61338da7380d3ade992d5a112a100 to your computer and use it in GitHub Desktop.
Rust Struct Lifetime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
use std::fmt; | |
#[derive(Copy, Clone)] | |
pub struct Type<'a> { | |
mime_type: &'a str, | |
} | |
impl<'a> Type<'a> { | |
pub(crate) const fn new(mime_type: &'a str) -> Self { | |
Self { mime_type } | |
} | |
pub const fn mime_type(&self) -> &'a str { | |
self.mime_type | |
} | |
} | |
impl fmt::Debug for Type<'_> { | |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
f.debug_struct("Kind") | |
.field("mime_type", &self.mime_type) | |
.finish() | |
} | |
} | |
pub struct Infer<'a> { | |
mmap: Vec<Type<'a>>, | |
} | |
impl<'a> Infer<'a> { | |
pub const fn new() -> Infer<'a> { | |
Infer { mmap: Vec::new() } | |
} | |
pub fn add(&mut self, mime_type: &'a str) { | |
self.mmap.push(Type::new(mime_type)); | |
} | |
pub fn get_first(&self) -> &'a Type { | |
&self.mmap[0] | |
} | |
} | |
fn main() { | |
let mut i = Infer::new(); | |
i.add("foo"); | |
println!("first: {:?}", i.get_first()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment