// sample-compile-error.rs: THIS FILE DOES NOT COMPILE, ON PURPOSE. // Free sample from 99helpers.com/file-types/rs // // It holds two of the most common Rust compile errors, so you can check // how an editor, a CI job or a code viewer reports them. The program is // otherwise harmless: fixed, it only prints a few lines. // // Try it: rustc --edition 2024 sample-compile-error.rs // Expected: error[E0382] and error[E0502], and no executable. fn shout(text: String) -> String { text.to_uppercase() } fn main() { // 1. E0382, "borrow of moved value". Passing `greeting` by value moves // it into `shout`, so `main` can't use it afterwards. // Fix: call `shout(greeting.clone())`, or make `shout` take `&str`. let greeting = String::from("hello"); let loud = shout(greeting); println!("{loud} (was {greeting})"); // 2. E0502, "cannot borrow as mutable because it is also borrowed as // immutable". `first` borrows from `numbers`, and `push` may move the // list's contents to a new place in memory while `first` still // points at the old one. // Fix: copy the value out first: `let first = numbers[0];` let mut numbers = vec![1, 2, 3]; let first = &numbers[0]; numbers.push(4); println!("first is {first}"); }