Skip to content

Instantly share code, notes, and snippets.

@bojand
Created July 17, 2020 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojand/f0a61338da7380d3ade992d5a112a100 to your computer and use it in GitHub Desktop.
Save bojand/f0a61338da7380d3ade992d5a112a100 to your computer and use it in GitHub Desktop.
Rust Struct Lifetime
#![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