DEV Community

Magnus Stråle
Magnus Stråle

Posted on

Rust - first impressions

I have started playing with Rust and here are some observations from a newbie point-of-view. My reasons for looking into Rust is that it's a modern language and does away with manual memory management without resorting to garbage collection or ref counting. This was a concept that I found very intriguing. How the heck did they solve that?

First of all I need a goal whenever I start to pick up a new language, otherwise I tend to just skip over the difficult parts. In this case I picked up the book "The Ray Tracer Challenge" which starts from zero and shows you how to build a ray tracer (bonus points for doing it TDD-style!). Since I've never done any ray tracing work before it turned out to be quite educational in that aspect as well.

Keep in mind that this is written by a Rust newbie, so please excuse any mistakes/errors in this post (and let me know about them!).

So what about memory management in Rust?

The idea is to have strict rules about memory ownership and lifetime management. Memory is automatically allocated for your data structures and released when your structure goes out of scope. For local variables this is really straight-forward but for data that gets passed around it does get a bit more complicated. You can either let a method borrow your data (ownership still remains within the calling code) or you can move your data (ownership is passed to the called method, meaning that you cannot reference that data in the calling code after the method call).

There is also the concept of copying the data, which allows you to pass a separate copy of the data and still keeping the original intact. This is done by implementing a special Copy Trait (similar to interfaces in other languages).

It does take quite a bit of trial-and-error in the beginning to get the borrow / move semantics correct, randomly adding '&' and 'mut' here and there. The good news is that the compiler does give quite helpful messages and there is a lot of helpful documentation online, as well as a very helpful community.

Anything besides memory management?

  • Speed! Both the tooling as well as your compiled code runs really fast. I haven't done any serious benchmarking but the code/compile/test cycle is at least as quick as anything else I've worked with.
  • Documentation. I did mention helpful online documentation and I must say that Rust does shine in this department. Yes - the language is still under active development so there are changes happening that might trip you up, but the docs have saved me every time. Not only language and API documentation but also a very nice book (aka 'The Book') teaching you the basics on Rust.
  • Immutability by default. If you want to have mutable data you have to explicitly state so, otherwise your data is safe and immutable.

Is it all good in Rust-land?

There are of course things that annoys me...

  • No support for overloading. I like to have the possibility to overload a method that takes somewhat different set of parameters. Not possible in Rust - you have to use another name. This is particularly annoying for the new method (the name is a convention for constructor-like methods) where I really want to have a couple of versions for convenience.
  • Strict separation between data and code. You use a struct to define the data for your objects and a separate impl section where you put your methods. Not saying that I completely dislike this, but it's different to what I'm used to.
  • Operator overloading. There is a lot of repetition (types!) for defining operator overloads. Can we at least get rid of the "type Output = ..."?

What are my conclusions so far?

The systems programming concern clearly shines thru in a number of design decisions for Rust which makes it feel very "close to the metal"/C-like when coding. But surprisingly enough this is coupled with a number of high-level constructs (lambdas, functional programming support etc) which enables you to write very expressive and concise code. The safety aspects are also very nice, it even looks like Microsoft is considering using Rust for this very reason.

Rust is definitely a language that I will invest more time in, especially since I will dig into WebAssembly in the future (which Rust fully supports). Will it be my go-to language for quick hacks/side projects? Not sure - C# is still what I'm most comfortable with.

Top comments (4)

Collapse
 
bmitch profile image
Bill Mitchell

Was it hard to translate the Ray Tracer Challenge book to rust (assuming the book is not using rust)?

Looks like an interesting way to help learn a language.

Collapse
 
magnusstrale profile image
Magnus Stråle

Hi! The code in the book is very pseudo-code and rather procedural. It's not hard to get something working, but I've been struggling a bit with making it idiomatic Rust. Will put it on Github next week for the world to see ;)

Collapse
 
bmitch profile image
Bill Mitchell

Would love to see the code once it's up! Thanks!

Thread Thread
 
magnusstrale profile image
Magnus Stråle

Sorry for the excessive delay, but here it is - dev.to/magnusstrale/the-ray-tracer...