# A weekend with Rust, and the index that stopped cloning

> I finally spent a weekend writing Rust rather than reading about it. Notes on what the borrow checker is actually for, why the compiler errors deserve their reputation, and the Cargo change that made the first five minutes bearable.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-04-22
- Tags: Rust, Go, C#, Performance
- URL: https://deyanpeev.com/posts/a-weekend-with-rust

Rust has been on my list for years, in the way things stay on a list. I read
about it, agree that it sounds correct, and then use something I already know.
This month I finally sat down with it for a weekend and wrote something small
that does real work - a tool that walks a directory of markdown files, parses
front matter, and reports on it.

These are beginner's notes. I'm writing them down because what surprised me
wasn't what I had been told would surprise me.

# The first five minutes got better

A small thing first, because it was the first thing I noticed.

Cargo has historically kept its index of crates.io as a git repository, cloned
locally. That repository indexes every crate in existence, and it had reached the
size where updating it was a visible pause before anything else could happen.

Rust 1.68, in March, stabilised a sparse registry protocol. Instead of cloning
the whole index, Cargo fetches over HTTP only the entries for the crates you
depend on. It isn't the default for crates.io yet (that's planned for 1.70), so
you opt in:

```bash
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
cargo build
```

The difference on a cold machine is obvious. It's a small piece of
infrastructure work, but it changes the impression the language makes in the
first minute.

# The borrow checker is about aliasing

I had absorbed the idea that Rust's ownership rules are about memory safety, and
they are, but that framing made them feel like a tax for a problem I don't have.
I write C# and TypeScript, I have a garbage collector, and memory is not what
breaks.

It clicked when I realised the rules are about **aliasing**, and aliasing bugs
I know very well. One reference mutating a structure while another reads it
happens in every language. The object passed into a function that mutated it,
the array a caller kept a reference to, the shared configuration that one
module edits at startup. I've debugged all three in garbage-collected
langauges. They were correctness bugs, nothing to do with memory safety, and
they were hard to find because nothing in the code said who was allowed to
change what.

Rust makes that a compile error. One mutable borrow, or any number of immutable
ones, never both. Once I stopped reading that as a memory rule and started reading
it as "say who owns this", the errors stopped feeling arbitrary.

The other half is cleanup. A value is dropped when its owner goes out of scope,
so files close and locks release without a `using` or a `defer`, and without a
collector deciding when.

# The compiler deserves its reputation

```
error[E0502]: cannot borrow `posts` as mutable because it is also borrowed as immutable
  --> src/main.rs:34:5
   |
32 |     let first = &posts[0];
   |                  ----- immutable borrow occurs here
34 |     posts.push(next);
   |     ^^^^^^^^^^^^^^^^ mutable borrow occurs here
36 |     println!("{}", first.title);
   |                    ----- immutable borrow later used here
```

Three locations, named in the order that explains the problem: where the borrow
started, where it conflicted, and why the first one was still alive. I've spent
a lot of my career with compilers that tell you a type does not match and leave
the rest as an exercise. This is a different standard, and I think it's most of
why the learning curve is survivable.

The fix is rarely clever. My first instinct on every error was to add a
`.clone()`, and that works, and it's almost always the wrong answer. The right
answer is nearly always to restructure so the two things aren't live at the
same time. Take the index instead of the reference, or finish reading before
writing. Every time I did that the code got simpler, and that was the surprise
of the weekend. Mostly the borrow checker was telling me my structure was
muddled, and it was right.

# Not rewriting anything

I am not converted, and I'm not planning to rewrite anything. I pick the
language where the ecosystem is already pointed at the problem I have, and for
a backend service that's still C# or TypeScript, with Go when I want a static
binary.

What I got is a better vocabulary. "Who owns this value and who is allowed to
change it" is a good question to ask in any language, and Rust is the only one
I have used that refuses to compile until you answer it. I've caught myself
asking it in C# since.

The other thing I'll keep is the compiler errors, as a standard to hold other
tools to. I can't see a technical reason why a TypeScript error couldn't point
at three locations and explain which one is still live.
