FAQ - Help and questions

What reasons do teams give for choosing Rust, and what do they cost?

#why-rust#porting#basicspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Teams usually name four reasons: memory safety without a garbage collector, steady latency, one self-contained executable, and compiler checks that make refactoring safer. Each has a price: a steeper first month, slower builds and fewer libraries in some fields. This thread shows each reason in code.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

ReasonWhat it means in practiceWhat it costs
Memory safety without a garbage collectorUse-after-free, double free and data races in safe code are compile errorsYou learn ownership and borrowing before the code compiles
Steady latencyNo collector pauses; memory is freed at a point you can read in the codeYou decide who owns what, and sometimes use Rc, Arc or indices
One self-contained executablecargo build --release produces a single file with no runtime to installExecutables are larger than you may expect (see the thread on executable size)
Refactoring with the compiler's helpChange a type and the compiler lists every place that must changeCompile times are longer than in an interpreted language
One build toolCargo builds, tests, runs and fetches dependenciesSome 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:

src/main.rsrust
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();
}
text
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:

src/main.rsrust
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());
}
text
10

Common 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet