Again, one article just for writing practice. :)
Rust-lang release alpha 1.0 today. Rust aims to be a systems level programming language to replace C and C++. I hit Rust-lang about two month ago, and found it’s a funny language. Then I read some Rust code and also wrote a hobby project with it.
There are several feature attract me:
Write low-level code with safety guarantees
Rust have the concept of onwership. For the resource in computation(this is usually refer to memory, file handler etc), the should be an owner. Rust try to solve the common errors caused by pointers in C/C++, such like dangling pointer, unfree pointer, double free issues. The borrow checker in compiler will keep the resource onwership move correctly with some rules. for more details please refer to offical guide. So as a newbie, writing code in Rust code seems always fighting with compiler. We can not just write code and then fix the memory later, the compiler refuses to accept anything which maybe unsafe, but this also make me think more about the code and design.
By the way, the error hints from compiler is very helpful, this is not like C++(specially templates got in).
There are some comparisons between Go and Rust, Gc is optional in Rust, compare Rust with Go is not sensible.
Recent changes of removing runtime make Rust lower level. There are even some hobby projects writting OS with Rust, refer to this and this.
High level abstraction for system programming
As a modern system programming, Rust is surprisingly expressive. I like the Ruby syntax, Rust has the same similarly mind-blowing effect. Rust carry some functional programming concepts, these make code looks just simple and elegant. Let’s have some trivial code snippet:
// construct array with 0 3 6 ...
let v = (0..10us).map(|x| x * 3).collect::<Vec<_>>();
for i in v.into_iter() { println!("{}", i); }
// construct array with random values
use std::rand;
let v = Vec::from_fn(10, |_| rand::random::<uint>());
Pattern match is so elegant:
match number {
1 => println!("One!"),
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
13...19 => println!("A teen"),
_ => println!("Ain't special"),
}
Colsures, reminds me with Ruby’s block:
fn main() {
let captured_value = 7u;
let closure = |&:argument| {
println!("I captured this: {}", captured_value);
println!("Argument passed was: {}", argument);
true
};
println!("Closure returned: {}", closure("a string"));
}
Almost every statement is an expression, this means that the statement returns a value. Blocks are also expression. This is good thing, we may write less “return”! Mixing with pattern match ends with a better sugar.
Of course, nice syntax doesn’t really mean real expresiveness, There are more abstraction tools in Rust, like traits, macro definiation, generic types etc. I have tried some macros for testing in rust-scm.
High Speed
I have found my favorite interpeter language, it’s Ruby. But in real world, we need to write some code need critical time performance. For this kind of task, Rust maybe a good choice. Benchmarks show Rust is almost as fast as C++.
Community
The Rust have a small, but exciting, openly community. The language have been evolving several years, most design discussion are open source. The core team seems nice.
Have a try for Rust.