4 ms·
> I would consider using it as a safer alternative to C or C++ I would, too. But there’re problems for which C or C++ is just faster because of language/compil
by Const-me 9y ago
> I would consider using it as a safer alternative to C or C++
I would, too. But there’re problems for which C or C++ is just faster because of language/compiler extensions like SIMD intrinsic or OpenMP.
Also Rust can’t be used for GPU code; strictly speaking C can’t either, but practically CUDA, C++ AMP, OpenCL are very close to C and/or C++.
Also if you have to deal with lots of pointer-based data structures (trees, graphs, etc)., and it’s not just on the lowest level you can abstract away behind a safe API, I don’t think Rust is safer. To get performance comparable to C++, it’s necessary to use unsafe rust for raw pointers, and IMO modern C++ is safer that unsafe Rust.
- steveklabnik 9y agoSIMD is coming in eight weeks, incidentally.
- Const-me 9y agoGood to know! But even if it’ll become available in 2 months, it’ll take some time to develop Rust libraries on top of them. E.g. in various performance-critical C++ projects I have used https://github.com/Microsoft/DirectXMath https://github.com/Microsoft/DirectXMath and https://eigen.tuxfamily.org https://eigen.tuxfamily.org Both are quite large projects with many man-years spent to develop, they saved quite a lot of my development budget.
- steveklabnik 9y agoYes and no, a lot of libraries have been built on top of the feature while it was unstable. It’s been in nightly for quite a while at this point. Of course, more libraries are needed as well, but there’s already some higher level stuff. See https://github.com/AdamNiederer/faster https://github.com/AdamNiederer/faster for example. And const generics, coming to nightly near the end of the year, will be another step up. It's true overall that numeric stuff is a weakness, but we'll get there!
- pcwalton 9y agoHow is modern C++ safer than unsafe Rust? In Rust, the borrow check is still active even in unsafe blocks. In C++, however, the complex rules around when destructors are called, combined with references, are a constant use-after-free footgun that you can never eliminate. Rust projects that use unsafe code are empirically safer than C++ projects. See, for example, the Servo style system as used in Firefox.
- Const-me 9y ago> the borrow check is still active even in unsafe blocks When you deal with pointer-based structures (trees, graphs, etc.) you’ll use raw pointers for them. Raw pointers are not borrow checked, and the complete list of pointer-related potential bugs applies to unsafe Rust, you can use after free, double free, mess with pointer arithmetic so you’re out of bounds, etc. > In C++, however, the complex rules around when destructors are called, combined with references, are a constant use-after-free footgun that you can never eliminate. C++ ecosystem has lots of stuff that help writing correct code despite unsafe language. Debug builds use special version of heap that fills freed memory with a magic number, this help to catch most UAF bugs very early in the development. There’re runtime tools like valgrind and asan. There’re static analysis tools like PVS studio, clang static analyzer, and coverity. Unsafe Rust has none of them. Not even a debug heap. > Rust projects that use unsafe code are empirically safer than C++ projects. Survival bias: people who need raw pointers and other performance-related features like SIMD and manual RAM layout don’t pick Rust for their projects.
- burntsushi 9y agoRespectfully, you're missing one of the most important parts of Rust. Rust isn't just about reducing or eliminating the use of `unsafe`, it's the ability to build an API that might use `unsafe` internally but is safe to use externally. You can't do that with C++. I also think many of your points over generalize. Finite state machines are graphs for example, but I've never needed to use raw pointers to achieve the performance required. Similarly, I definitely need SIMD, and I use Rust for that.
- 9y ago