First impression of Rust

I wrote Rust for a week. It was best of the languages I have used.

I remember when I was writing C++

C++ is very robust language. What I felt when writing C++ as an author of a library was, basically, that the type system can impose constraints as much on user code. But the more precise I pursue, the more difficult it was to express. Naturally it cause let code more strange for general C++ programmer and for also myself.

Can you guess why did it need such many code? It's because of my sense of instability. e.g. many way of initialization, many pattern of type conversion, and many worrying of destruction (including resistance of throwing exception). And what made me tired were a ton of compilation error, difficult of template meta programming, or another meta programming stage different from that -- powerful preprocessors --. (I know recently C++ became more good in such point of course. auto, nullptr, range-based-for ...etc. Oh, but these are off main of this article.)

Here is one simple example what Rust can do but not C++.

Think about dividing a cheese. kunsei_cheese.png

// C++
int main() {
    Cheese c { 100 }; // here is 100 grams of cheese.

    vector<Cheese> cs = divide(move(t), 2);  // divide it into 2.

    cout << cs[0].weight() << endl; // 50 grams
    cout << cs[1].weight() << endl;
    cout << c.weight() << endl; // <- Already c was moved. So what should be displayed?
}
// Rust
fn main() {
  let c = Cheese { weight: 100 };

  let cs = divide(c, 2);

  println!("{}", cs[0].weight());
  println!("{}", cs[1].weight());

  println!("{}", c.weight()); // <- rustc does not allow this.
}

Rust can provide this robustness just by representing the arguments of the function divide().

I worked with rustc

Like above example, Rust reduced the time for writing code that be written to get rid of sense of insecurity. Because rustc pointed out where has a problem, or where is useless. At times, rustc let me notice that someone else might read it could lead to confuse.

Where I made bug in Rust is these.

  • Miscalculation of Array index.
  • unwrap() I used without thinking.
  • unsafe{} I used without thinking.

... It seems as if it has been patterninged. unsafe{} was really unsafe.

I usually use Ruby. When I write similar code in Ruby, I have to consider more many possibility of making bug.

  • Inequality between String and Symbol
  • Unintended updating of variables
  • Miscontroll of memory consumption

Anyway, Rust is so good

The other day, I received a lot of comments about Rust's attraction.

"Why does Rust look attractive?" Hashnode

Including these comments, I would like to convey the goodness of Rust to my team.

Thank you.

Comments (1)

Mark's photo

Interesting to read your experience!

I still make unwrap bugs :-(

I like that Rust makes me think about what could be empty of could have failed. That's easy to forget in languages like Java.

But what usually goes wrong for me, is that I first want to see if the "happy flow" works (by just unwrapping anything that could fail). And only later do I want to add the error handling. But at that point, I don't know anymore which unwraps were actually safe, and which were temporary.

And which things can be unwrapped also depends on the level of 'stability' you need...

I use comments for the moment, but if anyone has a better way...

As for unsafe, I've found that if it seems I should use unsafe, really I should usually just search another approach 90% of the time.