From 07d7855f477e7ce3baa9208be3c26878cfb4e045 Mon Sep 17 00:00:00 2001 From: Stephan Rehfeld Date: Wed, 22 Apr 2026 18:36:00 +0200 Subject: [PATCH] CODE DOES NOT Compile You can't trick the borrow checker, by putting references into another struct --- src/main.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 378aec2..e152092 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,16 +19,6 @@ impl RGB { } } -struct Container<'a> { - value: &'a RGB, -} - -impl<'a> Container<'a> { - fn new(value: &RGB) -> Container { - Container { value } - } -} - struct Image { width: usize, height: usize, @@ -69,6 +59,20 @@ impl Image { } } +struct Container<'a> { + value: &'a RGB, +} + +impl<'a> Container<'a> { + fn new(value: &RGB) -> Container { + Container { value } + } +} + +fn foo<'a>(image: Image) -> Container<'a> { + Container::new(image.get(0, 0)) +} + fn main() { let image = Image::white_image(640, 480); @@ -76,5 +80,8 @@ fn main() { let container = Container::new(c); + let container1 = foo(image); + println!("{}", container.value.blue); + println!("{}", container1.value.blue); }