published on in development Rust

Learning Rust from scratch

So you want to be a programmer? Great! So do I. Now let’s pick up Rust together. It’s an immensely powerful language that provides the control of a systems language combined with the modern features of a scripting language. This site documents my own progress and any musings on my learnings along the way, so it’s by no means a tutorial. I don’t know this stuff any better than the next beginner, so don’t take any of this seriously.

Hello World?

I first started my quest for knowledge of sytems programming as a kid in the 80’s so I’m not a complete n00b. Chances are, you aren’t either if you’re picking up Rust. This makes the Rust documentation intensely annoying. There’s a lot of quick progress from “Hello World”, and then suddenly I look behind me and it turns out I fell off a cliff somewhere and the ground is rushing towards my face at an alarming rate.

That’s mainly because Rust has its own paradigms on how code should be organized and how components interact. Forget everything you know from other languages and look at Rust as if it’s entirely new. This blog contains my personal notes and nothing more. I publish them only in the hope that my rumblings may be of use to other learners.

So to get the obvious stuff out of the way: I’ll be working from Linux/FreeBSD while coding. If you’re following along on a Mac or Windows, consult the Rust official docs for installation isntructions. I just pulled Rust from the OS repo so that I have cargo and rustc at my fingertips. I’m also assuming you have Git working, but that’s by no means a requirement. Nothing stops you from coding without version control. Especially with small bits of disposable experiments there’s really no need for formal version control.

Cargo cult

Cargo is Rust’s package manager. Its use is ubiquitous in the community so I’m adopting it right from the start. Sure you can write Rust code without it, but I won’t because package management is a good habit to get into when the language has such first-rate support for it. If you’re coming from Python, NodeJS or PHP you’ll feel right at home using it. If you’re coming from C++ or C, read up on Cargo a bit first. It makes sharing your code ridiculously easy and you’ll have the same easy access to a large library of pre-existing code from others that’s completely free for you to use. For now, I’m just using it as a simple local build tool (note: no need to fiddle with Makefiles, Cargo has you covered here).

$cargo init helloworld

This gives you a project structure under the hellworld directory right where you invoked it. The most interesting bits about this, at least to me, are the fact that this instantiates a working Git repository and a working Hello World example program for free. No typing required. If you type cargo run from your newly minted project, the example will compile and run. If it doesn’t, your Rust kit is incomplete or broken.

Where to begin?

fn main() {
    println!("Hello, world!");
}

This is the main.rs file that defines the default entrypoint function and invokes a macro to print a line to stdout.

Now on to something useful

Now that that’s out of the way, I’m actually interested in writing a command line utility that creates hard disk image files for my retro emulation hobby. The scope of this starts with:

  • Properly accepting command line parameters
  • Creating new files and/or opening existing ones.
  • Writing to these files individual bytes at a time.
  • Handling errors in a proper way.

None of these subjects are anywhere near my level of skill with Rust right now, so let the challenge begin.