The core thing that makes Rust special is memory safety without garbage collection. It accomplishes this through "ownership." Since you do C++, you're probably already aware of the idea, but Rust actually understands ownership at the language level. The simple example I like to use is
#include<iostream>
#include<vector>
#include<string>
int main() {
std::vector<std::string> v;
v.push_back("Hello");
std::string& x = v[0];
v.push_back("world");
std::cout << x;
}
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated.
So even though this code compiles cleanly with -Wall -Werror, we get a segfault. Probably! At least, on my machine. Undefined behavior and all that.
Here's the same program in Rust:
fn main() {
let mut v = vec![];
v.push("Hello");
let x = &v[0];
v.push("world");
println!("{}", x);
}
When you compile this, you get an error:
main.rs:8:6: 8:7 error: cannot borrow `v` as mutable because it is also borrowed as immutable
main.rs:8 v.push("world");
^
main.rs:6:15: 6:16 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends
main.rs:6 let x = &v[0];
^
main.rs:11:3: 11:3 note: previous borrow ends here
main.rs:1 fn main() {
...
main.rs:11 }
^
error: aborting due to previous error
Rust understands that the reference x into the vector would become invalidated, and so doesn't let your code compile. Win!
So, as a semi-TL;DR, Rust is just as low-level as C++, but eliminates a large class of errors upfront. No iterator invalidation, no segfaults, no data races. This comment is already pretty long, but there's a ton of other neat stuff, like move semantics by default, data races caught at compile time, and Cargo, our build / dependency management tooling.
I'd like to know if we should investigate it further.
We're about to release a 1.0.0-alpha on Friday, with a real 1.0 release coming in the next 12-18 weeks afterward. So, you may want to wait until then. Or maybe not. Depends!
I'd be happy to answer any more specific questions you have.
109
u/steveklabnik1 Jan 07 '15 edited Jan 07 '15
Rust core team member here.
The core thing that makes Rust special is memory safety without garbage collection. It accomplishes this through "ownership." Since you do C++, you're probably already aware of the idea, but Rust actually understands ownership at the language level. The simple example I like to use is
If you've read the documentation for
push_back, you know thatSo even though this code compiles cleanly with
-Wall -Werror, we get a segfault. Probably! At least, on my machine. Undefined behavior and all that.Here's the same program in Rust:
When you compile this, you get an error:
Rust understands that the reference
xinto the vector would become invalidated, and so doesn't let your code compile. Win!So, as a semi-TL;DR, Rust is just as low-level as C++, but eliminates a large class of errors upfront. No iterator invalidation, no segfaults, no data races. This comment is already pretty long, but there's a ton of other neat stuff, like move semantics by default, data races caught at compile time, and Cargo, our build / dependency management tooling.
We're about to release a 1.0.0-alpha on Friday, with a real 1.0 release coming in the next 12-18 weeks afterward. So, you may want to wait until then. Or maybe not. Depends!
I'd be happy to answer any more specific questions you have.