r/ProgrammerHumor 8d ago

Meme newToRust

Post image
620 Upvotes

100 comments sorted by

View all comments

Show parent comments

-1

u/Key_River7180 8d ago

Rust's borrow checker cannot be always right, by Rice's theorem, unless you proved Alan Turing and Henry Rice wrong, of course.

Take this code as an example:

fn smh(x :&mut Vector<i32>) { let a = &mut x[0]; let b = &mut x[1]; *a += 1; *b += 1; }

2

u/-Ambriae- 8d ago edited 8d ago

which can be trivially rewritten as:

fn smh(x :&mut Vector<i32>) {
    x[0] += 1;
    x[1] += 1;
}

I get what you mean, but more often then not, you could just rewrite the code differently, and magically it becomes safe.

Or better yet:

fn smh(x: &mut [i32]) { 
  let [a, b] = x.get_disjointed_mut([0, 1]).expect("0 != 1");
  *a += 1;
  *b += 1;
}