Absolutely new in Rust

⚓ Rust    📅 2026-01-06    👤 surdeus    👁️ 3      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Absolutely new in Rust

Hi !
As the title says, I'm a pure novice in Rust, and in this kind of programming language in general with strict types and compiling process.
I wanted to learn something like C or derivative, but I remembered about Rust, and after some readings I thought it could be a good choice.
So I started from scratch, with a medium project about creating mazes or labyrinths. But even if I conscientiously started to read the book, I think I need some feedback to know if I'm in the good way.

After some reading, I've wrote some basic stuff, but I need some kind of an approval I'm in the good direction : does what I wrote is a good way to design in Rust ? Does it is a more efficient way ? If so, could you explain me ?

enum CardinalPoint {
	North,
	East,
	South,
	West,
}

#[derive (Debug)]
struct Cell {
	north_wall: Wall,
	east_wall: Wall,
	south_wall: Wall,
	west_wall: Wall,
}

#[derive (Debug, Default)]
struct Wall {
	built: bool,
	breakable: bool,
}

impl Cell {
	fn new () -> Self {
		Cell {
			north_wall: Wall { built: false, ..Default::default() },
			east_wall: Wall { built: false, ..Default::default() },
			south_wall: Wall { built: false, ..Default::default() },
			west_wall: Wall { built: false, ..Default::default() },
		}
	}
	fn build_wall (mut self, c: Option<CardinalPoint>) -> Self {
		match c {
			None => return self,
			Some(CardinalPoint::North) => { self.north_wall.built = true },
			Some(CardinalPoint::East) => { self.east_wall.built = true },
			Some(CardinalPoint::South) => { self.south_wall.built = true },
			Some(CardinalPoint::West) => { self.west_wall.built = true },
		}
		self
	}
	fn has_wall (&self, c: CardinalPoint) -> bool {
		match c {
			CardinalPoint::North => self.north_wall.built,
			CardinalPoint::East => self.east_wall.built,
			CardinalPoint::South => self.south_wall.built,
			CardinalPoint::West => self.west_wall.built,
		}
	}
	fn is_wall_breakable (&self, c: CardinalPoint) -> bool {
		match c {
			CardinalPoint::North => self.north_wall.breakable,
			CardinalPoint::East => self.east_wall.breakable,
			CardinalPoint::South => self.south_wall.breakable,
			CardinalPoint::West => self.west_wall.breakable,
		}
	}
	fn is_dead_end (&self) -> bool {
		let walls = [self.north_wall.built, self.east_wall.built, self.south_wall.built, self.west_wall.built]
			.iter().filter(|&&x| !x).count();
		println!("There is {} walls built", walls);
		walls < 2
	}
}

fn main () {
	let mut cell_1 = Cell::new()
					.build_wall(None)
					.build_wall(Some(CardinalPoint::East))
					.build_wall(Some(CardinalPoint::South))
					.build_wall(Some(CardinalPoint::West));
	cell_1 = cell_1.build_wall(Some(CardinalPoint::North));

	println!("Cell Object --> {:?}", cell_1);
	println!("North Wall is built ? --> {:?}", cell_1.has_wall(CardinalPoint::North));
	println!("East Wall is beakable ? --> {:?}", cell_1.is_wall_breakable(CardinalPoint::South));
	println!("Dead End --> {:?}", cell_1.is_dead_end());
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed