Advent of Code - Day 2 - Increasing and decreasing (Rust)

This task is about testing if integers in a list are increasing or decreasing. I’ll try Rust, and I assume I need nerves of steel for this exercise.

Setting Up the Development Environment

Rust comes in a container, too. Here’s the Dockerfile:

# Dockerfile
FROM rust:latest

# Install additional tools you might need
RUN apt-get update && apt-get install -y \
    git \
    curl \
    wget \
    build-essential

# Set the working directory
WORKDIR /workspace

# Copy project files (if any)
COPY . .

# Default command
CMD ["bash"]

And here’s the devcontainer.json to develop inside that container made of steel:

{
    "name": "Rust Development Container",
    "build": {
        "dockerfile": "../Dockerfile"
    },
    "customizations": {
        "vscode": {
            "extensions": [
                "rust-lang.rust-analyzer",
                "serayuzgur.crates",
                "tamasfe.even-better-toml"
            ]
        }
    },
    "remoteUser": "root",
    "features": {
        "ghcr.io/devcontainers/features/rust:1": {}
    }
}

Rust also requires a Cargo.toml file that defines your project and dependencies:

# Cargo.toml
[package]
name = "rust-docker-project"
version = "0.1.0"
edition = "2024"

[dependencies]
# none, ehehe

That’s all we need. Build it with docker build and connect VS Code. The source file is located inside the src folder. You can run your code like this

cargo build
cargo run

The algorithm itself isn’t that tricky. You’ll find everything on Github.

Whats up, Rust?

Rating: 4/12 – Wouldn’t use it freely

Don’t get me started. I didn’t know that Rust has so many strange rules. There’s this interesting concept of ownership: values belong to a variable as long as you don’t assign that variable to another variable. If you pass a value from one vector to another, you can’t access it in the original vector anymore.

And then there are many important operators scattered across the code, like:

  • & for borrowing (referencing a value without taking ownership)
  • * for de-referencing (accessing the value a reference points to)

What I do like, though, is the error handling. Rust uses Ok and Err to make handling errors much cleaner and explicit:

Err("Value not valid")
Ok(())

This approach forces you to handle potential issues early on, which is great for reliability.

See you next day…