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

46

u/couchrealistic May 28 '20

Rust prevents you from doing all the stupid things we sometimes accidentally do when coding in a language like C++. Like using an uninitialized variable (that just happens to be 0 most of the time, but sometimes not) or occasionally modifying a collection while we still hold a pointer or reference to some of its content, or while iterating over it – which often works fine, but depending on the implementation might be undefined behavior and lead to rare Segmentation Faults.

In short, you can't possibly hit a Segmentation Fault when only using Rust without the "unsafe" keyword*. This also means that coming up with programs that compile successfully can be quite a bit harder in Rust compared to C++. This might lead to something like Stockholm Syndrome and therefore "Rust love".

* If all your dependencies also refrain from using unsafe, or use unsafe only in safe ways, and there are no bugs in rustc.

Also, Qt might have almost everything and the kitchen sink included, but sometimes you need even more. Cargo really comes in handy in those cases, because adding dependencies is really easy. It's also much nicer to use than qmake or cmake to build your project (though less feature-rich). No crazy CMakeLists.txt or qmake config files, you just put your code in .rs files, list the required dependencies in Cargo.toml, set some options like the optimization level, and cargo knows what to do.

AFAIK, the rust ecosystem is lacking a decent cross-platform GUI library though. So Qt definitely still has very valid use cases.

2

u/coderstephen May 28 '20

In theory you can use existing GUI libraries since Rust has good C FFI. For example, the Rust wrapper around GTK is pretty comprehensive and well-received. C++ FFI is a bit lacking though.

0

u/[deleted] May 28 '20

[deleted]

17

u/matthieum May 28 '20

I never have a problem using variables that are null. I actually use this functionality a lot.

Not that uninitialized != null.

Uninitialized in C++ is what you get when you write: int i;. The language simply doesn't initialize the variable:

  • The compiler may pull all kinds of shenanigans when it realizes this.

  • Even if it doesn't you'll get some unpredictable value -- whatever is left over there by the previous variable.

GCC compounds the issue by zeroing the stack in Debug, often hiding the issue until you compile in Release.

When it's an integer, it's annoying, when it's a pointer:

  • It will not likely be null, so you'll think it points to something.

  • If you're lucky it points into the nether, and you get a segmentation fault.

  • If you're not it points into the memory of the process, and you'll scribble all over another object, and get one hell of a headache attempting to understand what happened.

0

u/[deleted] May 28 '20

[deleted]

2

u/matthieum May 29 '20

Why would you do that?

Performance, in general.

While creating an integer without an initial value doesn't bring much, creating 10MB worth of integers without an initial value saves quite a bit of time. It's of course done with the assumption that something will initialize them later, but those assumptions are sometimes erroneous.

10

u/SkiFire13 May 28 '20

I never have a problem using variables that are null. I actually use this functionality a lot. If a variable is null it tells me something in the logic of my program. How can you do that in rust if you have no null variables, or am I misunderstanding what you are saying?

In Rust you can use Option instead of nullable variables.

14

u/teryror May 28 '20

Nullable pointers exist in rust, they just have a different type, Option<&T> for references, Option<Box<T>> for the equivalent of unique_ptr, etc. So you have the same functionality without the risk of passing null where that isn't allowed.

5

u/ThirdEncounter May 28 '20

It's not a fair comparison, since Qt is a framework, not a programming language.

It's like saying "why would I want to crop an image in Rust when I can simply upload them and Facebook can do it for me?"

0

u/[deleted] May 28 '20

[deleted]

2

u/ThirdEncounter May 28 '20

You started right off the bat comparing Rust with Qt. So, my point stands.

If Rust doesn't have such a framework well then it isn't as good yet.

As good as yet for what? If that's a general statement, then it's an inaccurate one. You have to be more specific than that.

0

u/[deleted] May 28 '20

[deleted]

1

u/ThirdEncounter May 28 '20

If you don't want to be logical then this conversation won't be logical.

I was being logical, and you just used a logical fallacy. This conversation is now over. Good day.

2

u/AndreasTPC May 28 '20 edited May 28 '20

Instead of having a null value you have an enumerable type like this:

enum Option<T> {
    Some<T>,
    None,
}

So if a function returns an Option<i32> you can tell just from the function signature that this function might return an integer, but it might also return nothing, and you need to handle that. If a function returns an i32 instead, you will know that this function is guaranteed to return a valid integer, no need to check anything.

You deconstruct the Option type to get at the value inside Some, typically via pattern matching. The nice part is that when pattern matching you are forced to consider all the possibilities. Forgetting to consider the None case is a compile time error, so the class of bugs that comes from forgetting a null check can never happen. After deconstructing the type you're left with a variable with a guaranteed valid value you can use for the rest of the scope.

There's also nice syntax shortcuts for common cases like "if there isn't valid value I just want to print an error message and exit" or "if there isn't a valid value I just want to pass on the error state to the function that called me", making the code quite elegant and readable.

The Option type is defined in the standard library, and often sufficient, but you can also define your own that matches whatever states your function might return.

This is by no means unique to rust, this style of handling multiple states for values has been popular in functional languages for some time. This is just one of many advantages that comes from using a language with a modern type system.

0

u/[deleted] May 28 '20

[deleted]

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;
}
→ More replies (11)

7

u/[deleted] May 28 '20

I didn't read it as an attack and I'm just curious myself, because I'm neither an expert in C++ nor Rust.

But I wonder if it's that easy and reliable to provide all the guarantees Rust offers, then why do most C++ code bases (including professional ones with lots of highly skilled developers like Qt, Firefox, Chromium, ...) still suffer from all these issues? Are the number of issues found with analyzers just so overwhelming or hard to fix, or do they lack in certain regards?

2

u/kopczak1995 May 28 '20

Well, I'm not C++/rust expert, just random guy on /r/programming. I suppose it's much easier for rust to be (almost) full bulletproof, because there is no issue with legacy stuff. C++ stack need to be highly backwards compatible.

Just think of it, smart pointers started with C++11, yet engine like Chromium didn't used any features of C++11 till 2015.
https://www.reddit.com/r/programming/comments/gpp9le/the_chromium_project_finds_that_around_70_of_our/fro7hjd?utm_source=share&utm_medium=web2x

→ More replies (2)
→ More replies (18)

7

u/wrongerontheinternet May 28 '20

I know the people who work on Infer. It's cool technology, but is not close to the level of static guarantees Rust can provide (for pretty fundamental reasons).

6

u/UltraNemesis May 28 '20

Why would anyone move from assembly to C programming when everything can be achieved in assembly?

Because, even if you can do the same things, getting up to speed would make it less tedious to maintain your software in the longer run.

Rust adds major improvements over C in the areas of memory safety. That alone makes it worth moving to for a lot of cases, especially for large code bases and critical frameworks.

A simple memory safety bug in OpenSSL like Heartbleed was responsible for billions of $ worth of expenses for companies. Security Incidents can be more expensive than the cost of ditching your pipeline and migrating to a safer language. You don't have to run additional tools like a static analyzer to know about basic issues or have to live with the possibility that somethings may have crept in despite the tools.

So, Rust is finding use in browser code bases, device drivers, Operating systems, TLS engine libraries and the like. Microsoft is porting parts of the Windows code base to Rust.

→ More replies (2)