Zeeshan Adil
Book a call
← All writing
Cover of Rust for C# Engineers: From Garbage Collection to Ownership and Lifetimes

Rust for C# Engineers: From Garbage Collection to Ownership and Lifetimes

by Zeeshan Adil·in Systems ProgrammingUpdated 6.5 hr read
Listen to this articleRust for C# Engineers: From Garbage Collection to Ownership and Lifetimes 16 min · read by an AI voice
0:00 / 16:12

A book-length guide to Rust for engineers who already know C#. Fifteen chapters on ownership, borrowing, lifetimes, traits, generics, errors, concurrency, async and unsafe, each one anchored on the .NET model you already carry in your head.

THE SHORT VERSION

  • A guide that ports your .NET mental model to Rust instead of teaching syntax from scratch.
  • Rust asks at compile time what the CLR answers at run time: who owns a value and how long a reference lives.
  • Fifteen chapters cover ownership, borrowing, lifetimes, traits, generics, errors, concurrency, async and unsafe.
  • Each chapter stands on its own after the foundations, so you can jump to what you are building.

Your first real Rust error will probably be borrow of moved value. You wrote something that would be fine in C#, the compiler refused it, and the honest reaction is that the language is being difficult for no reason.

It is not. It is asking you a question that the CLR answers for you at run time, and it wants the answer before the program runs.

That question leads into ownership, borrowing, and lifetimes. Once those ideas fit together, the differences in strings, error handling, concurrency, and async become easier to follow.

So this is not a syntax tour. You already know how to write a loop. This is an attempt to port your mental model. You have one for .NET, built over years, that tells you where objects live, who cleans them up, what happens when two threads touch the same list, and roughly what the JIT will do with your generics. Most of that model is still useful. Some of it is actively wrong in Rust, and the wrong parts are what make people bounce off the language in week two.

What this assumes about you

You can write C# methods, classes, and collections, and you have used interfaces, generics, and exceptions. Some familiarity with value types, reference types, and garbage collection will help. You do not need to know Rust or have written C++.

The later chapters use .NET threads, async/await, LINQ, and IDisposable as comparisons. If those are unfamiliar, take the foundations first and return to the relevant chapter when you need it. The examples use .NET 9 and C# 13 unless a section says otherwise.

How to read it

Your first goal is to explain three things: who owns a value, who is borrowing it, and how long that borrow can remain valid. You can learn those without first understanding every detail of the allocator or JIT.

Choose a route that matches what you need:

Your goalReading routeSave for later
Write your first Rust programRead the six foundations chapters in order, then errors, optional values, and the first-month plan.Detailed layout measurements, dispatch internals, and unsafe implementation.
Design a library or understand an existing APIAfter ownership, borrowing, and lifetimes, read traits, generics, and error handling.Threading and async until the API needs them.
Build concurrent or async workStart with the foundations, then read interior mutability, thread safety, and async.Performance tuning until you have a working example to measure.

Within each chapter, follow one example through before reading every aside. Predict its result, run it, and change one thing. For a deliberately broken example, being able to explain the compiler error is the useful outcome.

Here is the full chapter order for when you want the longer explanation.

Part I, Foundations. These six chapters are load-bearing and the rest of the book assumes them.

  1. The trade Rust is actually making
  2. Reading Rust when you already know C#
  3. Where your data actually lives
  4. Ownership, and the rule that replaced the collector
  5. Borrowing, and why the compiler counts your references
  6. Lifetimes are not object lifetime

Part II, Working knowledge. How Rust does abstraction, and the two types you will touch in every function you write.

  1. Traits are interfaces that do not own your types
  2. Generics that compile to what you would have written by hand
  3. Errors are values, and you have to look at them
  4. The null that is not there

Part III, Concurrency and systems. Where ownership starts paying for itself, and where async gets harder than it is in C#.

  1. Interior mutability, or breaking the rules on purpose
  2. Fearless concurrency is a claim the type system makes
  3. Async without a runtime in the box

Part IV, Under the hood. Advanced, and skippable on a first pass. Come back when you want the machinery.

  1. Zero-cost abstractions, and where the cost actually lands
  2. Unsafe is a smaller word than you think

Then a chapter on what I would actually do in a first month, the sources worth reading next, and the questions people ask.

If you only read part of it, read Part I. Ownership, borrowing and lifetimes are not three features, they are one idea seen from three angles, and almost every later chapter is downstream of them.

CHAPTERS

Sources and further reading

Each chapter links its sources at the claim they support. This is the shorter list of things worth reading in full, rather than consulting.

The official material, in the order it becomes useful

The .NET side of each comparison

Going deeper

On whether any of this is worth it

Continue learning

A path rather than a pile.

Start by making ownership automatic. Write a small tool that reads files and prints something, and clone whenever the compiler complains. The goal is not efficiency, it is getting to the point where moves stop surprising you.

Then take the clones out one at a time and let the borrow checker explain why each existed. This is where owned against borrowed types stop being trivia and become a decision you make deliberately.

Once borrowing is comfortable, learn traits properly, especially the conversion traits, iterators and the derive macros. Most of what feels like magic in a good Rust codebase is a trait implementation you have not read yet.

After that, pick a direction based on what you build. For services, go to async: futures, Tokio, and why the Send bound propagates through everything. For libraries and tools, go to generics and dispatch.

Leave unsafe until last, and only if you need it.

Two things worth understanding alongside all of this, because they explain more than their size suggests: how memory layout and alignment decide what a struct actually costs, and how a tracing collector decides what is still reachable. The second is what makes the first chapter land.

Frequently asked questions

Do I need to know C or C++ first?

No, and coming from C# is arguably an advantage. You already think in types and abstractions rather than in manual allocation, so what you are actually learning is where values live and who owns them, not pointer arithmetic. A C++ background helps with layout intuition and actively hurts with move semantics, because a C++ move leaves a valid, usable object behind and a Rust move does not. C++ engineers spend their first week discovering that the moved-from variable is gone at compile time rather than merely empty.

Is Rust actually faster than C#?

For most application code, not by enough to justify a rewrite. Modern .NET is a genuinely fast runtime, the JIT is very good, and if your bottleneck is a database query or a network round trip then the language is not what is slow.

Rust can be attractive for tail latency, memory budgets, and startup time. Removing a tracing collector removes one source of pauses, but allocation, destruction, locks, page faults, I/O, and scheduling still affect latency. Memory use depends on representation and allocator behavior, and .NET Native AOT changes the startup comparison. Benchmark the workload you intend to ship.

Throughput, tail latency, memory use, and startup time are separate measurements. Decide which constraint matters before comparing implementations.

How long before I am productive?

Two to four weeks to write useful code if you are already senior. Two to three months before the borrow checker stops being the main thing you are thinking about. The curve is unusually front-loaded, which is why the language has a reputation for being hard: most of the difficulty arrives in the first fortnight rather than being spread out, and people judge it from inside that window.

What flattens it fastest is resisting the urge to be efficient early. Clone, get the program working, and take the clones out once you understand why each one was there.

Can I call Rust from .NET?

Yes, and it is the sensible way to adopt it. Build the Rust side as a cdylib exposing extern "C" functions, then call it with DllImport, or with the LibraryImport source generator on .NET 7 and later.

The boundary is a C ABI, which carries no lifetime or ownership information, so marshalling and ownership across it are entirely your responsibility. The usual rules apply: decide which side allocates and which side frees, do not pass Rust String or Vec across directly, and expose a free function for anything Rust allocated. Taking the single hot component out of an existing service is a far better first project than a rewrite, and it lets you keep everything the .NET ecosystem is good at.

Should I start with an async web service?

No. It stacks async, lifetimes, trait bounds and an unfamiliar framework on top of ownership all at once, and when you get stuck you will not be able to tell which of the four is responsible. Write something synchronous and small first. You will get to the web service two weeks later and it will take an afternoon.

Does Rust have a garbage collector?

Rust has no mandatory tracing garbage collector. The compiler inserts drop operations, but their timing depends on runtime control flow; for Rc and Arc, the last strong reference determines when the payload is destroyed. Libraries can also implement tracing collectors when a design calls for one.

Rc and Arc do reference counting where shared ownership is genuinely needed. That is automatic memory management, but it is not a tracing collector and the difference matters: reference counting cannot collect cycles, so two values holding counted handles to each other leak. Weak is the fix, and it is a fix you have to remember to apply. This is the one place where .NET's collector is straightforwardly better than what Rust gives you, and it is worth saying plainly rather than defending.

Does the borrow checker change how I design, or is it just a stricter compiler?

It changes the design, and this is the part that is hard to convey before you have felt it.

In C#, ownership is a documentation concern. Who is allowed to mutate this list, who is responsible for disposing this handle, whether this array is safe to hand out or needs a defensive copy: all real questions, all answered in comments, conventions and code review. Rust moves those questions into the signature, so they get answered when you write the function rather than when someone violates the assumption six months later.

The practical effect is that data flow becomes a tree far more often than it does in C#, because a graph of mutable references is expensive to express and a tree is free. That constraint pushes designs toward clearer ownership than most of us would have arrived at unaided, which is the argument people mean when they say the borrow checker made them a better C# programmer too.

What is the ecosystem genuinely missing compared to NuGet?

Be realistic about this, because it is the most common reason a sensible Rust project fails.

Rust is strong where it grew up: networking, parsing, serialization, command line tools, cryptography, embedded, WebAssembly, and increasingly data infrastructure. Web frameworks and HTTP clients are good. Serialization through serde is better than anything in .NET.

It is weaker in enterprise integration. Object relational mapping exists but nothing has the maturity or the tooling of Entity Framework Core. Desktop UI is unsettled. Cloud provider SDKs have improved a great deal but coverage lags the .NET SDKs. Anything touching the Microsoft ecosystem specifically, from identity libraries to Office formats to line of business connectors, is thin or absent.

If your service is mostly translating HTTP into SQL, .NET has more of what you need and a rewrite buys you very little. If it is mostly moving bytes, parsing protocols or holding state in memory under latency pressure, the gap closes and often reverses.

Is it worth learning if I will not use it at work?

I think so, for a reason that has nothing to do with employment. Rust forces you to be explicit about ownership, aliasing and lifetime, and those concepts exist in every language you already use. They were simply invisible because the runtime was handling them.

After a few months of Rust, the aliasing bugs in C# code start to look obvious rather than unlucky. You notice which methods take a collection and quietly keep a reference to it. You notice which disposal paths are only correct by convention. Whether or not you ship a line of Rust, the model transfers, and that is a better argument for learning it than performance is.

KEEP GOING

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.