CODE DOES NOT Compile

You can't trick the borrow checker, by putting references into another struct
This commit is contained in:
Stephan Rehfeld 2026-04-22 18:36:00 +02:00
parent 352085f8d9
commit 07d7855f47

View File

@ -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 { struct Image {
width: usize, width: usize,
height: 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() { fn main() {
let image = Image::white_image(640, 480); let image = Image::white_image(640, 480);
@ -76,5 +80,8 @@ fn main() {
let container = Container::new(c); let container = Container::new(c);
let container1 = foo(image);
println!("{}", container.value.blue); println!("{}", container.value.blue);
println!("{}", container1.value.blue);
} }