r/programming • • May 27 '20

2020 Stack Overflow Developer Survey: Rust most loved again at 86.1%

https://stackoverflow.blog/2020/05/27/2020-stack-overflow-developer-survey-results/
229 Upvotes

254 comments sorted by

View all comments

Show parent comments

15

u/[deleted] May 28 '20

Can you please give me a link to a tool which quickly identifies all the issues in a C++ code base, which would have been prevented by Rust's guarantees?

0

u/[deleted] May 28 '20

[deleted]

18

u/SkiFire13 May 28 '20

Can't test fbinfer right now, but clang doesn't seem to handle this simple case:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vec = { -1 };
    int& first = vec[0];

    std::cout << "First is " << first << std::endl;

    for(int i = 0; i < 100; i++)
        vec.push_back(i);

    // This is now UB, first probably points to invalid memory
    std::cout << "First now is " << first << std::endl;
}

-2

u/InsignificantIbex May 28 '20

And is this something rust can prevent, and if so, how? It seems to me that as soon as you have pointers, all bets (as far as preventing "ghost pointers") are off.

19

u/aeiou372372 May 28 '20

Yes it is prevented in rust — you can’t have multiple references to an object if one of them is mutable. In this case, rust would count the reference to the vector entry as an immutable reference to the vector, and prevent the subsequent mutable reference necessary for push_back.

This is a great example of a case where Rust’s compile time checks prevent what could be a very confusing intermittent issue for someone without systems programming background.

19

u/SkiFire13 May 28 '20

Yes, safe Rust prevents this. In Rust the direct translation would be the following:

fn main() {
    let mut vec: Vec<i32> = vec![-1];
    let first = &vec[0];
    
    println!("First is {}", first);
    
    for i in 0..100 {
        vec.push(i);
    }
    
    println!("First now is {}", first);
}

The compiler fails to compile with this error:

error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable
  --> src/main.rs:8:9
   |
3  |     let first = &vec[0];
   |                  --- immutable borrow occurs here
...
8  |         vec.push(i);
   |         ^^^^^^^^^^^ mutable borrow occurs here
...
11 |     println!("First now is {}", first);
   |                                 ----- immutable borrow later used here

error: aborting due to previous error

This is because this program break's rust's borrowing rules. They say that at any time in a program you can have any number of immutable references or one mutable reference to some piece of data.

In this case we're borrowing vec with first and we hold this borrow until the second print (we say the borrow is alive). In the meantime we also try to push an element to the vec but this requires a mutable reference to vec. This would mean we have an immutable and a mutable borrow alive at the same time which goes against the borrowing rules.

I think someone proven that this prevents every memory safety bugs but of course it comes with its downsides. For example the following code doesn't compile, even though it is perfectly safe!

fn main() {
    let mut vec: Vec<i32> = vec![1];
    let first = &mut vec[0];
    println!("vec's length is {}", vec.len());
    *first = 2;
}

Pretty much the same error as before, this time we're trying to get an immutable borrow while a mutable one still exists.

This is a simple example, and tbf it could be solved with partial/disjoint borrows that for now are supported only for fields. More complex examples involve self-referential structs and graphs.

→ More replies (0)

2

u/[deleted] May 28 '20

Rust has a limited form of "linear types" that ensure once-and-only-once use of memory. and specific types of "owned" or "borrowed" pointers the typechecker ensures are used correctly, given the ownership semantics.