Re:When Rust Makes Sense, or The State of Typed Languages
by seanwilson on Monday September 28, 2015 @07:19AM
(#10289818)
As an analogy, you can write safe code in assembler as well if you and your team are incredibly disciplined and only interact with libraries written with a similar amount of discipline. The appeal of alternatives is you can write safe code with a fraction of the discipline and mental effort required.
Re:When Rust Makes Sense, or The State of Typed Languages
by claudius on Monday September 28, 2015 @07:34AM
(#10289867)
There is no extra “discipline“ or “mental effort” required to write safe C++. Don’t use new/delete/free/malloc and don’t use operator&. Of course, you can say that we might as well remove them from the language entirely, but I really don’t think it takes extra effort not to use them.
Re:When Rust Makes Sense, or The State of Typed Languages
by seanwilson on Monday September 28, 2015 @07:41AM
(#10289890)
There are countless ways of causing undefined behaviour in C++ (not including memory management mistakes) as well as platform specific behaviour that you don't see in other languages. It's a very complex language to master compared to alternatives.
Re:When Rust Makes Sense, or The State of Typed Languages
by keyneus on Monday September 28, 2015 @12:41PM
(#10291359)
C++14:
auto p = std::make_unique<int>(5);
auto q = std::move(p);
std::cout << *p << std::endl;
Segfault using the “safe” C++ features. I'm a fan of modern C++, but it's not safe (in a Rust sense) even if you stick to C++11/C++14 features.
Re:When Rust Makes Sense, or The State of Typed Languages
by steveklabnik on Monday September 28, 2015 @01:39PM
(#10291755)
For comparison, the same thing in Rust:
fn main() {
let p = Box::new(5);
let q = p;
println!("{}", p);
}
gives, upon compiling: error: use of moved value: `p` [E0382]
println!("{}", p);
^
note: in this expansion of format_args!
note: in this expansion of print! (defined in <std macros>)
note: in this expansion of println! (defined in <std macros>)
help: run `rustc --explain E0382` to see a detailed explanation
note: `p` moved here because it has type `Box<i32>`, which is moved
by default
let q = p;
^
help: if you would like to borrow the value instead, use a `ref`
binding as shown:
let ref q = p;
error: aborting due to previous error
Running `rustc --explain E0382` or going to https://doc.rust-lang.org/nightly/error-index.html#E0382 gives you an extended error message as well.
It can. You just need to avoid those performance optimizations that were en vogue in the 90s/early00s. Luckily modern computers (and the more modern C++ standards) can handle value semantics just fine and you don't need to put pointers into vectors anymore because directly putting the data there would be too costly, etc.
If you only use the new (C++11 up) smart pointers (avoiding new/delete) you've essentially achieved 80% of Rust's safety model. Add rigorous application of const correctness and you're good to go. Rust still adds a few important things but until its eco system is matured (and there is real tooling) modern C++ is a pretty good alternative.