C# to Rust — A Practical Tutorial for .NET Developers
Rust 1.95.0 · Edition 2024 · Cargo Workspace
A complete, runnable Cargo workspace teaching Rust to experienced C# / .NET developers. Every concept is shown side-by-side with its C# equivalent, each example is self-contained, and all code compiles on Rust 1.95.0.
Table of Contents
- Prerequisites & Setup
- Repository Structure
- How to Run Examples
- C# → Rust Concept Map
- Module Reference
- 1-Week Study Plan
- Glossary
- Capstone Project
- Recommended Resources
Prerequisites & Setup
Install Rust 1.95.0
# Install rustup (skip if already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # Linux/macOS
# or download from https://rustup.rs on Windows
# Install Rust 1.95.0 and set as default
rustup install 1.95.0
rustup default 1.95.0
# Verify
rustc --version # rustc 1.95.0 (...)
cargo --version # cargo 1.95.0 (...)
Clone and Build
git clone <this-repo>
cd c-sharp-to-rust-learning
# Check all crates compile (fast — no linking)
cargo check --workspace
# Build everything in release mode
cargo build --workspace --release
# Run all tests across the workspace
cargo test --workspace
VS Code Setup (Recommended)
# Install rust-analyzer extension
code --install-extension rust-lang.rust-analyzer
# Optional: better error lenses
code --install-extension usernamehw.errorlens
Repository Structure
c-sharp-to-rust-learning/
├── Cargo.toml ← Workspace root (shared deps, edition 2024)
├── README.md ← This file
│
├── 01_getting_started/
│ └── src/bin/
│ ├── hello_world.rs ← println!, format!, basic I/O
│ ├── cargo_basics.rs ← Cargo commands cheat-sheet
│ └── rustup_info.rs ← Toolchain management
│
├── 02_syntax_and_basic_types/
│ └── src/bin/
│ ├── variables_mutability.rs ← let, mut, shadowing, inference
│ ├── scalar_types.rs ← int/float/bool/char, overflow
│ ├── compound_types.rs ← tuples, arrays, slices
│ ├── strings.rs ← &str vs String, UTF-8
│ ├── constants_statics.rs ← const, static, const fn
│ └── control_flow.rs ← if/loop/for/match/if let/let-else
│
├── 03_ownership_borrowing_and_moves/
│ └── src/bin/
│ ├── ownership.rs ← Three ownership rules, Drop
│ ├── moves.rs ← Move semantics, partial moves
│ ├── copy_clone.rs ← Copy vs Clone traits
│ └── borrowing.rs ← &T / &mut T, borrow rules, NLL
│
├── 04_references_and_lifetimes/
│ └── src/bin/
│ ├── lifetimes.rs ← 'a annotations, struct lifetimes
│ ├── lifetime_elision.rs ← Three elision rules, use<>
│ └── dangling_refs.rs ← Use-after-free prevention
│
├── 05_structs_enums_and_pattern_matching/
│ └── src/bin/
│ ├── structs.rs ← Named/tuple/unit structs, impl
│ ├── enums.rs ← ADTs, Option<T>, Result<T,E>
│ ├── pattern_matching.rs ← match, guards, bindings, nested
│ └── option_result.rs ← Combinators, ?, From, conversion
│
├── 06_traits_generics_and_impl_blocks/
│ └── src/bin/
│ ├── traits.rs ← Trait definition, default methods
│ ├── generics.rs ← fn/struct generics, where, assoc types
│ ├── trait_objects.rs ← &dyn Trait, Box<dyn>, fat pointer
│ └── advanced_traits.rs ← Operator overloading, Deref, Index
│
├── 07_error_handling/
│ └── src/bin/
│ ├── panic_basics.rs ← panic!, assert!, catch_unwind
│ ├── result_patterns.rs ← ?, chaining, collect, partition
│ ├── thiserror_example.rs ← #[derive(Error)], #[from], chains
│ └── anyhow_example.rs ← anyhow!, bail!, context(), downcast
│
├── 08_collections_and_iterators/
│ └── src/bin/
│ ├── vec_collections.rs ← Vec, VecDeque, BTreeMap, BTreeSet
│ ├── hashmap_hashset.rs ← HashMap CRUD, entry API, HashSet
│ ├── iterators.rs ← All iterator adapters & consumers
│ └── custom_iterators.rs ← impl Iterator, Fibonacci, BST
│
├── 09_closures_and_functional_patterns/
│ └── src/bin/
│ ├── closures.rs ← Fn/FnMut/FnOnce, capture modes
│ ├── function_pointers.rs ← fn type, dispatch tables
│ └── functional_patterns.rs ← map/filter/fold, currying, Cow
│
├── 10_modules_crates_and_workspaces/
│ └── src/bin/
│ ├── modules.rs ← mod, pub, use, crate/super/self
│ ├── visibility.rs ← pub(crate), pub(super), pub(in)
│ └── cargo_features.rs ← #[cfg], features, build.rs
│
├── 11_testing_and_documentation/
│ ├── src/
│ │ ├── lib.rs ← Library with doc tests, proptest
│ │ └── bin/doc_examples.rs ← Doc comment styles
│ └── tests/integration_tests.rs ← Integration tests
│
├── 12_memory_management_and_smart_pointers/
│ └── src/bin/
│ ├── box_pointer.rs ← Box<T>, recursive types
│ ├── rc_arc.rs ← Rc/Arc, Weak, reference cycles
│ ├── cell_refcell.rs ← Cell<T>, RefCell<T>, Rc<RefCell<T>>
│ ├── mutex_rwlock.rs ← Mutex<T>, RwLock, Arc<Mutex>
│ └── deref_drop.rs ← Deref coercion, RAII, Drop order
│
├── 13_concurrency_threads_and_channels/
│ └── src/bin/
│ ├── threads.rs ← thread::spawn, join, scope
│ ├── channels.rs ← mpsc, clone tx, try_recv
│ └── shared_state.rs ← Barrier, Condvar, OnceLock, Atomic
│
├── 14_async_await_and_tokio/
│ └── src/bin/
│ ├── async_basics.rs ← async fn, .await, join!, select!
│ ├── tokio_tasks.rs ← spawn, try_join_all, Semaphore
│ ├── streams.rs ← Stream trait, adapters, channels
│ └── pin_unpin.rs ← Pin<T>, Unpin, stack/heap pinning
│
├── 15_macros/
│ └── src/bin/
│ ├── macro_rules.rs ← macro_rules!, repetition, recursion
│ ├── builtin_macros.rs ← println!, assert!, vec!, env!, cfg!
│ └── derive_macros.rs ← #[derive], #[cfg], attributes
│
├── 16_unsafe_rust/
│ └── src/bin/
│ ├── unsafe_blocks.rs ← 5 unsafe superpowers, safe wrappers
│ └── raw_pointers.rs ← *const/*mut, arithmetic, Box round-trip
│
├── 17_ffi_and_interop/
│ └── src/bin/
│ └── ffi_basics.rs ← extern "C", CString/CStr, #[no_mangle]
│
├── 18_performance_and_benchmarking/
│ ├── src/bin/performance_tips.rs ← Allocation, zero-cost iterators, Cow
│ └── benches/
│ ├── string_bench.rs ← Criterion string benchmarks
│ └── iter_bench.rs ← Criterion iterator benchmarks
│
├── 19_cli_apps/
│ └── src/bin/
│ ├── clap_basics.rs ← clap derive, subcommands, ValueEnum
│ └── file_tool.rs ← wc/grep/head clone, anyhow, stdin
│
├── 20_web_api_project/
│ └── src/bin/
│ └── web_server.rs ← axum CRUD API, State, Json, Path
│
├── 21_advanced_patterns/
│ └── src/bin/
│ ├── builder_pattern.rs ← Fluent builder, validated, phantom-typed
│ ├── newtype_pattern.rs ← Units, validated types, Deref
│ ├── state_machine.rs ← Enum-based state machine
│ └── type_state.rs ← Phantom-typed compile-time states
│
└── 22_csharp_to_rust_migration_guides/
└── src/bin/
├── gc_vs_ownership.rs ← GC pauses vs RAII, Weak<T>
├── classes_vs_structs.rs ← Class → struct+impl, interface → trait
├── linq_vs_iterators.rs ← Every LINQ method mapped to Iterator
├── async_model_comparison.rs ← Task<T> vs Future, cancellation
└── exceptions_vs_results.rs ← throw/catch vs Result<T,E>, ?
How to Run Examples
Naming convention
Every example is a standalone binary. The bin name is the filename without .rs:
14_async_await_and_tokio/src/bin/streams.rs → cargo run --bin streams
21_advanced_patterns/src/bin/state_machine.rs → cargo run --bin state_machine
All commands below must be run from the workspace root (the folder containing the top-level Cargo.toml).
Discover available bins
# List every binary in the workspace:
cargo run --bin 2>&1 | grep " "
# Or just browse the source tree:
find . -path "*/src/bin/*.rs" -printf "%f\n" | sed 's/\.rs$//' | sort
# Windows (PowerShell):
Get-ChildItem -Recurse -Filter "*.rs" -Path "*/src/bin" | Select-Object BaseName | Sort-Object BaseName
Run a single example
cargo run --bin <bin_name>
# Examples:
cargo run --bin hello_world
cargo run --bin ownership
cargo run --bin async_basics
cargo run --bin streams
cargo run --bin state_machine
cargo run --bin performance_tips
cargo run --bin ffi_basics
Run in release mode (optimised, required for benchmarks)
cargo run --release --bin <bin_name>
# Example:
cargo run --release --bin performance_tips
Run an example from inside its module directory
cd 14_async_await_and_tokio
cargo run --bin streams
cargo run --bin async_basics
Pass arguments to CLI examples (module 19)
cargo run --bin clap_basics -- --help
cargo run --bin clap_basics -- greet --name Alice --times 3
cargo run --bin file_tool -- count src/bin/file_tool.rs
cargo run --bin file_tool -- find . --ext rs
The -- separator tells Cargo to stop parsing its own flags and forward the rest to your program.
Run tests
# Tests for one binary:
cargo test --bin async_basics
# Tests for one module (package):
cargo test -p async_await_and_tokio
# All tests in the workspace:
cargo test --workspace
# A specific test function by name:
cargo test --bin async_basics -- async_test
Run benchmarks (module 18)
# All benchmarks:
cargo bench -p performance_and_benchmarking
# One benchmark suite:
cargo bench -p performance_and_benchmarking --bench string_bench
cargo bench -p performance_and_benchmarking --bench iter_bench
Benchmarks always run in release mode automatically.
Quick-reference: all bins by module
| Module |
Bin name |
Topic |
| 01 |
hello_world · cargo_basics · rustup_info |
Getting started |
| 02 |
variables_mutability · scalar_types · compound_types · strings · control_flow · constants_statics |
Types & syntax |
| 03 |
ownership · moves · copy_clone · borrowing |
Ownership |
| 04 |
lifetimes · lifetime_elision · dangling_refs |
Lifetimes |
| 05 |
structs · enums · pattern_matching · option_result |
Structs & enums |
| 06 |
traits · generics · trait_objects · advanced_traits |
Traits & generics |
| 07 |
result_patterns · thiserror_example · anyhow_example · panic_basics |
Error handling |
| 08 |
vec_collections · hashmap_hashset · iterators · custom_iterators |
Collections |
| 09 |
closures · functional_patterns · function_pointers |
Closures |
| 10 |
modules · visibility · cargo_features |
Modules & crates |
| 11 |
doc_examples |
Testing & docs |
| 12 |
box_pointer · rc_arc · cell_refcell · mutex_rwlock · deref_drop |
Smart pointers |
| 13 |
threads · channels · shared_state |
Concurrency |
| 14 |
async_basics · tokio_tasks · streams · pin_unpin |
Async / Tokio |
| 15 |
macro_rules · derive_macros · builtin_macros |
Macros |
| 16 |
unsafe_blocks · raw_pointers |
Unsafe Rust |
| 17 |
ffi_basics |
FFI & interop |
| 18 |
performance_tips |
Performance |
| 19 |
clap_basics · file_tool |
CLI apps |
| 20 |
web_server |
Web API (axum) |
| 21 |
builder_pattern · newtype_pattern · state_machine · type_state |
Advanced patterns |
| 22 |
gc_vs_ownership · classes_vs_structs · linq_vs_iterators · async_model_comparison · exceptions_vs_results |
Migration guides |
| 23 |
encapsulation · inheritance_composition · polymorphism · operator_overloading |
OOP in Rust |
| 24 |
json_basics · advanced_serde |
Serde serialization |
| 25 |
tracing_basics |
Logging & tracing |
| 13+ |
send_sync |
Send + Sync (added to module 13) |
| 11+ |
unit_testing · proptest_basics |
Testing (added to module 11) |
C# → Rust Concept Map
Types
| C# |
Rust |
int, long, uint |
i32, i64, u32, u64 |
double, float |
f64, f32 |
bool |
bool |
char (UTF-16) |
char (Unicode scalar, 4 bytes) |
string (heap, immutable) |
String (owned heap) |