Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Foo<'a> {
- x: i32,
- /// y points to x
- x_ref: Option<&'a i32>
- }
- impl<'a> Foo<'a> {
- pub fn new(x: i32) -> Self {
- Self { x, x_ref: None }
- }
- pub fn set(&'a mut self) {
- self.x_ref = Some(&self.x);
- }
- fn x(&self) -> i32 {
- self.x
- }
- fn x_ref(&self) -> Option<&i32> {
- self.x_ref
- }
- }
- fn main() {
- let foo = Foo::new(10);
- foo.set();
- println!("{}", foo.x());
- println!("{:?}", foo.x_ref());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement