Rust vs C comes down to who checks memory use. In C you are responsible for it and the compiler mostly trusts you, while in Rust the compiler rejects code that could free memory twice, use it after it is freed, or race on it, at about the same native-code speed.
Choose Rust for new systems code where memory bugs would be costly. Choose C when you must match an existing C codebase, target a platform with no Rust toolchain, or need the smallest possible and most widely supported toolchain. The sections below show the differences with real compiler errors and documented behavior, not benchmark claims.
Rust vs C at a glance
| Aspect | C | Rust |
|---|---|---|
| Memory management | Manual: malloc and free | Ownership: memory is freed when the owner goes out of scope |
| Out-of-bounds read | Undefined behavior | Panics with index out of bounds |
| Dangling references | Compile and run, behavior undefined | Rejected at compile time in safe code |
| Data races | Undefined behavior, compiler does not check | Rejected at compile time in safe code |
| Signed integer overflow | Undefined behavior | Panics in debug builds, wraps in release builds |
| Escape hatch | Everything is already unchecked | unsafe blocks for five specific operations |
| Calling the other language | Native | unsafe extern "C" blocks |
How does memory safety differ between Rust and C?
C puts allocation, freeing and pointer validity entirely in your hands. The language definition says that undefined behavior places no restrictions on what the program does. Its examples include memory accesses outside array bounds, signed integer overflow and null pointer dereference, and compilers are not required to diagnose them. Here is a C function with a use-after-free (example code, not from the sources):
#include <stdlib.h>
int main(void) {
int *value = malloc(sizeof *value);
*value = 7;
free(value);
return *value; /* use after free: undefined behavior */
}
This is valid C syntax and typical compilers accept it, though some may warn. Whether it prints the right number, crashes or corrupts something depends on the allocator and the optimizer.
Rust takes the opposite approach. The Rust book gives three ownership rules: each value has an owner, there can be only one owner at a time, and when the owner goes out of scope the value is dropped. Rust calls drop automatically at the closing curly bracket, so you never write the free. Assigning let s2 = s1; moves ownership, and using s1 afterwards is a compile error (borrow of moved value: s1). The book explains that this is how Rust avoids a double free, "one of the memory safety bugs" where both pointers would try to free the same memory.
What does the Rust compiler reject that a C compiler accepts?
Two things in particular: dangling references and conflicting mutable access. The book's dangling example returns a reference to a local String that is dropped when the function ends. Rust refuses to compile it:
error[E0106]: missing lifetime specifier
--> src/main.rs:5:16
|
5 | fn dangle() -> &String {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
C has no ownership rule that would stop the equivalent function, which returns the address of a local variable, and using that pointer afterwards is undefined behavior. The borrowing rules are that at any given time you can have either one mutable reference or any number of immutable references, and references must always be valid. Break the first rule and you get error[E0499]: cannot borrow `s` as mutable more than once at a time.
The same rules prevent data races. The book defines one as two or more pointers accessing the same data at the same time, with at least one writing and no synchronization. Rust refuses to compile code that contains them. C leaves that to you, and the Rust Reference lists data races among the behaviors that are undefined.
Is Rust faster than C?
Not as a rule. Both languages compile ahead of time to native code and neither runs a garbage collector, so the large cost differences you see between, say, C and Python do not apply here. This article does not give benchmark numbers, because no source we opened supports a general claim in either direction.
What can be said from the documentation is where each language spends effort:
- Ownership is a compile-time idea. The book notes that Rust never automatically makes deep copies of your data, so any automatic copying can be assumed to be inexpensive.
- Rust checks indexing at runtime. Reading
v[99]on a three-element vector panics withindex out of bounds: the len is 3 but the index is 99. C performs no such check. Each approach has a cost: Rust pays a comparison and a possible panic, C risks a buffer overread. The Rust book describes the C case as undefined behavior that can leak whatever is stored after the data structure. - Overflow differs by build mode. In Rust debug builds, integer overflow panics. With
--release, it wraps using two's complement. The book calls relying on that wrapping an error, and listswrapping_*,checked_*,overflowing_*andsaturating_*methods for saying what you mean. In C, signed overflow is undefined, which lets the optimizer assume it never happens.
That last point is concrete. The C reference shows that return x + 1 > x; is either true or undefined behavior, so a compiler may fold it into a constant 1. Optimizers use undefined behavior to generate faster code, and the price is that a bug can change what the program means, not just crash it.
What does unsafe mean in Rust?
Rust is not a language where nothing can go wrong. The unsafe keyword unlocks five operations that the compiler does not check for memory safety: dereferencing a raw pointer, calling an unsafe function or method, accessing or modifying a mutable static variable, implementing an unsafe trait, and accessing fields of a union.
Two details matter. First, the book says unsafe does not turn off the borrow checker or other safety checks: a reference used inside unsafe code is still checked. Second, the Rust Reference says the undefined-behavior rules still apply inside unsafe blocks. Unsafe only means you, not the compiler, are responsible for avoiding it. Unsafe code is called sound if no safe code using it can trigger undefined behavior.
The practical difference from C is scope. In a C program, every line has this property. In a Rust program, the places to audit are the unsafe blocks and the safe interfaces built on top of them.
Can Rust and C work together?
Yes, and migrations usually do. To call C from Rust, declare the function in an unsafe extern "C" block. The book says functions declared there are generally unsafe to call, because other languages do not enforce Rust's rules and Rust cannot check them. Its example calls C's abs:
unsafe extern "C" {
fn abs(input: i32) -> i32;
}
fn main() {
unsafe {
println!("Absolute value of -3 according to C: {}", abs(-3));
}
}
To call Rust from C, mark a function pub extern "C" with #[unsafe(no_mangle)] so its name is not mangled. A memory bug on the C side of such a boundary is still undefined behavior for the whole program, and the Reference notes that undefined behavior affects code in other languages reached through FFI.
When should you pick C over Rust?
- You are extending existing C code. Adding a Rust module is possible, but a small patch to a large C project is usually better done in C.
- Your target has no Rust toolchain. Check the Rust platform support pages for your target, and compare them with what your vendor ships for C.
- You need to follow a standard or contract written in C terms, such as a driver interface or a plugin ABI. You can still implement it in Rust, but with
extern "C"glue. - The team knows C and the code is small and short-lived. Rust's learning curve is real: ownership, lifetimes and borrowing take time before larger programs compile.
When should you pick Rust over C?
- The code parses untrusted input such as network packets, file formats or images. Out-of-bounds reads and use-after-free are the classes of bug Rust is designed to remove from safe code.
- The code is concurrent. Data races are rejected at compile time in safe Rust.
- The program will be maintained for years by several people. Ownership and borrowing rules are written into the types, so a refactor that breaks them fails to compile instead of failing in production.
If you are comparing Rust with a higher-level language rather than C, see Rust vs Python: Key Differences and When to Use Each.
If the borrow checker keeps rejecting your code
Coming from C, the usual instinct is to reach for unsafe and raw pointers. Resist it. Read the error code and message, then restructure: return owned values instead of references to locals, shorten borrows so they end before the next mutable use, or split data into separate fields so they can be borrowed independently. Use unsafe only when you can state which rule you are upholding by hand.
Frequently asked questions
- Is Rust faster than C?
- Not in general. Both compile ahead of time to native machine code and neither has a garbage collector, so well-written programs in either language usually land in the same range. Rust adds runtime bounds checks on indexing, and C does not check anything by default. Which one wins depends on the program, the compiler and the code, so benchmark your own workload.
- Can Rust replace C?
- For new projects, often yes. For existing C code, usually not all at once. Rust can call C functions through extern blocks and expose Rust functions with the C ABI, so teams commonly rewrite one component at a time and keep the rest in C.
- Is Rust code ever memory unsafe?
- Yes, inside unsafe blocks. The Rust book lists five operations that only unsafe code can do, such as dereferencing a raw pointer, and the compiler does not check those for memory safety. Safe Rust, outside those blocks, is designed so that it cannot cause undefined behavior.
- Is C still worth learning?
- Yes. C is the language of a large amount of existing operating system, driver and embedded code, and the C ABI is what most other languages use to call each other. Learning it also shows you what Rust's ownership rules are protecting you from.
Sources
- The Rust Programming Language — What Is Ownership?
- The Rust Programming Language — References and Borrowing
- The Rust Programming Language — Unrecoverable Errors with panic!
- The Rust Programming Language — Data Types (integer overflow, array access)
- The Rust Programming Language — Unsafe Rust (including extern "C")
- The Rust Reference — Behavior considered undefined
- cppreference.com — C language: Behavior (undefined behavior)