Table of Contents
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:
1# Dockerfile
2FROM rust:latest
3
4# Install additional tools you might need
5RUN apt-get update && apt-get install -y \
6 git \
7 curl \
8 wget \
9 build-essential
10
11# Set the working directory
12WORKDIR /workspace
13
14# Copy project files (if any)
15COPY . .
16
17# Default command
18CMD ["bash"]
And here’s the devcontainer.json
to develop inside that container made of steel:
1{
2 "name": "Rust Development Container",
3 "build": {
4 "dockerfile": "../Dockerfile"
5 },
6 "customizations": {
7 "vscode": {
8 "extensions": [
9 "rust-lang.rust-analyzer",
10 "serayuzgur.crates",
11 "tamasfe.even-better-toml"
12 ]
13 }
14 },
15 "remoteUser": "root",
16 "features": {
17 "ghcr.io/devcontainers/features/rust:1": {}
18 }
19}
Rust also requires a Cargo.toml
file that defines your project and dependencies:
1# Cargo.toml
2[package]
3name = "rust-docker-project"
4version = "0.1.0"
5edition = "2024"
6
7[dependencies]
8# 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
1cargo build
2cargo 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:
1Err("Value not valid")
2Ok(())
This approach forces you to handle potential issues early on, which is great for reliability.
See you next day…
Summary
A write-up of the author's solution to Day 2 of the Advent of Code 2024 challenge, implemented in Rust. The article details the setup of a containerized Rust development environment using Docker and VS Code, briefly describes the day's task (checking integer sequences), and shares the author's first impressions of Rust's ownership model and explicit error handling.
Main Topics: Advent of Code Rust Programming Languages Docker VS Code Problem-Solving
Difficulty: intermediate
Reading Time: approx. 5 minutes