Created
March 3, 2017 13:28
-
-
Save jaheba/0fbde0562b60112943e5dc75560ccd60 to your computer and use it in GitHub Desktop.
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
struct Item { | |
list: *mut List, | |
} | |
impl Item { | |
fn new(list: *mut List) -> Self { | |
Item { list: list } | |
} | |
} | |
impl Item { | |
fn get_len(&self) -> usize { | |
unsafe { | |
(*self.list).values.len() | |
} | |
} | |
} | |
struct List { | |
values: Vec<Item>, | |
} | |
impl List { | |
fn new() -> Self { | |
List { values: Vec::new() } | |
} | |
fn push(&mut self) -> &Item { | |
let item = Item::new(self); | |
self.values.push(item); | |
&self.values.last().unwrap() | |
} | |
fn with_first() -> Self { | |
let mut list = List::new(); | |
list.push(); | |
list | |
} | |
} | |
fn main() { | |
let mut list = List::new(); | |
list.push(); | |
assert_eq!(list.values[0].get_len(), 1); | |
let list = List::with_first(); | |
assert_eq!(list.values[0].get_len(), 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment