Compare commits

...

2 Commits

Author SHA1 Message Date
Stephan Rehfeld
12a54c42da Image example to show Ownership 2026-04-15 18:58:16 +02:00
Stephan Rehfeld
bd260f1769 Drop and move behavior 2026-04-15 18:04:15 +02:00

View File

@ -1,23 +1,70 @@
struct Vector3 { #[derive(Copy, Clone)]
x: f32, struct RGB {
y: f32, red: u8,
z: f32, green: u8,
blue: u8,
} }
impl Vector3 { impl RGB {
pub fn new(x: f32, y: f32, z: f32) -> Vector3 { fn new(red: u8, green: u8, blue: u8) -> RGB {
Vector3 { x, y, z } RGB { red, green, blue }
}
fn black() -> RGB {
RGB {
red: 0,
green: 0,
blue: 0,
} }
} }
fn print(vec: Vector3) { fn white() -> RGB {
println!("Vector3( x = {}, y = {}, z = {})", vec.x, vec.y, vec.z); RGB {
red: 255,
green: 255,
blue: 255,
}
}
}
struct Image {
width: usize,
height: usize,
data: Vec<RGB>,
}
impl Image {
fn new(width: usize, height: usize, data: Vec<RGB>) -> Image {
if width * height != data.len() {
panic!("Width and height of image does not match amount of data.");
}
let img = Image {
width,
height,
data,
};
img
}
fn white_image(width: usize, height: usize) -> Image {
let mut data = Vec::new();
for i in 0..width * height {
data.push(RGB::white());
}
Image::new(width, height, data)
}
fn get(&self, x: usize, y: usize) -> RGB {
self.data[y * self.width + x]
}
} }
fn main() { fn main() {
let mut vec = Vector3::new(1.0, 0.0, 0.0); let image = Image::white_image(640, 480);
print(vec); let c = image.get(0, 0);
vec.y = 2.0;
} }