From f09f0efb7b729e69c0caab8f03fbb1916b40b3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Thu, 1 May 2025 09:29:47 +0200 Subject: [PATCH] feat: add display function This commit introduces a new `display` function: - It takes an immutable reference to a grid of bool values - It takes mutable reference to a String buffer. it writes to the buffer, `X` or `.` depending on the grid's bool element. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 24 ++++++++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e7f8e6..7135f57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 4 [[package]] -name = "GameOfLife_rs" +name = "GameOfLife" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7692178..ed6d414 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "GameOfLife_rs" +name = "GameOfLife" version = "0.1.0" edition = "2024" diff --git a/src/main.rs b/src/main.rs index e7a11a9..62deb32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,23 @@ -fn main() { - println!("Hello, world!"); +const WIDTH: usize = 30; +const HEIGHT: usize = 30; + +fn display(grid: &[[bool; WIDTH]; HEIGHT], buffer: &mut String) { + buffer.clear(); + + for row in grid { + for &cell in row { + buffer.push(if cell { 'X' } else { '.' }); + buffer.push(' '); + } + buffer.push('\n'); + } + + println!("{}", buffer); +} + +fn main() { + let grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT]; + let mut buffer: String = String::new(); + + display(&grid, &mut buffer); }