Rust lang
Hello rust
println!("Hi");Resources for learning rust lang
Section titled “Resources for learning rust lang”- Rustlings
- Exercism
- Reading books
Random Rust
Section titled “Random Rust”#[derive(PartialEq, Eq, Debug)]pub enum Direction { North, East, South, West,}
pub struct Robot { x: i32, y: i32, direction: Direction,}
use Direction::*;
impl Robot { pub fn new(x: i32, y: i32, direction: Direction) -> Self { Robot { x, y, direction } }
#[must_use] pub fn turn_right(self) -> Robot { Robot { direction: match self.direction { North => East, East => South, South => West, West => North, }, ..self } }
#[must_use] pub fn turn_left(self) -> Robot { Robot { direction: match self.direction() { North => West, East => North, South => East, West => South, }, ..self } }
#[must_use] pub fn advance(self) -> Self { match self.direction { North => Robot { y: self.y + 1, ..self }, East => Robot { x: self.x + 1, ..self }, South => Robot { y: self.y - 1, ..self }, West => Robot { x: self.x - 1, ..self }, } }
#[must_use] pub fn instructions(self, instructions: &str) -> Self { instructions .chars() .fold(self, |robot, instruction| match instruction { 'A' => robot.advance(), 'L' => robot.turn_left(), 'R' => robot.turn_right(), _ => robot, }) }
pub fn position(&self) -> (i32, i32) { (self.x, self.y) }
pub fn direction(&self) -> &Direction { &self.direction }}