Answer#
The reasons below are the ones the language is designed around. They are not guarantees for every project, and each comes with a cost that belongs in the decision. The Ruststudio team has no survey data on what teams choose, so this thread explains the mechanisms instead of quoting anyone.
| Reason | What it means in practice | What it costs |
|---|---|---|
| Memory safety without a garbage collector | Use-after-free, double free and data races in safe code are compile errors | You learn ownership and borrowing before the code compiles |
| Steady latency | No collector pauses; memory is freed at a point you can read in the code | You decide who owns what, and sometimes use Rc, Arc or indices |
| One self-contained executable | cargo build --release produces a single file with no runtime to install | Executables are larger than you may expect (see the thread on executable size) |
| Refactoring with the compiler's help | Change a type and the compiler lists every place that must change | Compile times are longer than in an interpreted language |
| One build tool | Cargo builds, tests, runs and fetches dependencies | Some fields (for example some GUI and data-science work) have fewer mature libraries than other languages |
The first row is the one people mean most often. Here is the same mistake in two languages: sharing a reference-counted value with another thread. In C++ the equivalent shared_ptr is thread-safe for its counter but not for the data, and the compiler does not check how you use it. In Rust the type that is only safe for one thread cannot be sent to another:
use std::rc::Rc;
use std::thread;
fn main() {
let shared = Rc::new(vec![1, 2, 3]);
let h = thread::spawn(move || println!("{}", shared.len()));
h.join().unwrap();
}error[E0277]: `Rc<Vec<i32>>` cannot be sent between threads safely
--> src/main.rs:6:27
|
6 | let h = thread::spawn(move || println!("{}", shared.len()));
| ------------- -------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | `Rc<Vec<i32>>` cannot be sent between threads safely
| | within this `{closure@src/main.rs:6:27: 6:34}`
| required by a bound introduced by this call
|
= help: within `{closure@src/main.rs:6:27: 6:34}`, the trait `Send` is not implemented for `Rc<Vec<i32>>`The message was shortened (the file name is shown as src/main.rs; it was compiled from a scratch file). Swapping Rc for Arc (the thread-safe counter) and putting the data behind a Mutex compiles and runs:
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let total = Arc::new(Mutex::new(0u32));
let handles: Vec<_> = (1..=4)
.map(|i| {
let total = Arc::clone(&total);
thread::spawn(move || *total.lock().unwrap() += i)
})
.collect();
for h in handles {
h.join().unwrap();
}
println!("{}", total.lock().unwrap());
}10Common mistakes#
- Choosing Rust for the speed of the language alone. Most of the benefit is in correctness and predictability; measured speed depends on the program (see the thread with a measured example).
- Counting only the cost of writing the first version. The compiler's checks move effort from debugging to writing, and that is the trade.
- Expecting every library you use today to exist in Rust. Check the libraries you depend on before deciding.
Related questions#
Does Rust remove all bugs?#
No. Logic errors, wrong requirements, panics from unwrap, deadlocks and arithmetic that overflows are still possible. See the thread on which bugs Rust prevents and which it leaves to you.
Is this reason enough to rewrite a working program?#
Usually not by itself. A working, tested program has value that a rewrite must earn back. See the thread on deciding whether a codebase is worth porting.
Sources#
- The Rust Book, "Fearless Concurrency": https://doc.rust-lang.org/book/ch16-00-concurrency.html
- The Rust Book, "Understanding Ownership": https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html