March 22, 2021

Learning Rust: Part 1

As a longtime C#/.NET developer, I wanted to learn a language that was a bit different than C#.  I played around a bit with Elixir, and while I found its functional programming paradigm enjoyable I couldn't get past the fact that it's a dynamically typed language.  I tried my hand at Golang but I didn't like its error handling pattern or the lack of generics. I had heard of Rust here and there over the years but given that it's a systems programming language I never really gave it much thought until about a month ago when I decided I was serious about learning it.

What drew me to learn Rust:

  • Rust is immutable by default, that is to say 'variables' cannot be changed once assigned unless you specify the mut modifier.
  • No more NULL/NullReferenceExceptions! I've fixed hundreds if not thousands of NullReferenceException in my many years as a C# developer. Rust uses the Option type to represent a valid/invalid value.
  • Rust has an elegant error handling mechanism
  • Rust is fast (and is usually at the top of the TechEmpower benchmarks.)
  • Rust has a very welcoming community (the Slack community's #beginners channel is fantastic!)
  • While it's not 'officially' a functional language, it does have features that functional languages have including:
    • the if statement is an expression and not a statement
    • exhaustive pattern matching
    • Result type for error handling

What is/has caused me to bang my head against the wall about Rust:

  • If you don't read The Rust Programming Language before writing any Rust code you're in for a world of hurt.
  • To no one's surprise, ownership & lifetimes. Rust is not a garbage-collected language. While you won't have to malloc/free memory yourself as if you were coding in C in 1972, you do have to understand some nontrivial rules about managing your application data's memory (will it be allocated on the stack or in the heap, what is it's scope/lifetime/etc. I can't say I've fully mastered/understood this yet, but when the compiler complains I can usually get it working without too much fuss.
  • Rust is not C# and it's not object-oriented. I think that's a great thing. I've been writing OO C# for so long that my first instinct is to take a very OO approach to solving problems. Not in the sense of using inheritance mind you. For example, I want to write a small service that could use one of several database (Sqlite, Postgres, etc.) based on configuration data. I thought I could use traits similarly to C# interfaces. After talking through it with someone in the Rust #beginners Slack channel they confirmed that traits != interfaces and that I really shouldn't do it this way.
  • Unlike C# with ASP.NET Core, Rust doesn't have a 'standard' web stack. I've found a couple of web frameworks worth delving into: Actix-Web and Rocket. There are lots of choices to make, and they can be overwhelming: which database access library to choose (sqlx, diesel, etc), should you use async and which async library should you use? Fortunately I came across this ebook that really helped me learn how to build basic web application/services with Rust.

I'm only a month into my journey of learning Rust, and some days I want to give up in frustration.  The next day I go back and make a little more progress.  Over time I expect that I will absorb enough understanding by reading books and writing working code such that I will feel confident in my ability to write quality Rust code.