From C# to Rust: Introduction

sebnilsson

Seb Nilsson

Posted on July 6, 2020

From C# to Rust: Introduction

Rust has been the most loved programming language for the last 5 years. This, and many other factors, made me interested in learning more about Rust, especially from the perspective of a C#-developer.

Why Rust?

In early 2019, a Microsoft engineer revealed that about 70% of all vulnerabilities in their products are due to memory safety issues. Memory safety is something you get for free with C# and .NET. But sometimes, you need more tools to squeeze out every last drop of performance.

It's also generally very beneficial to learn new languages, which could inspire us to find new ways of solving problems. To specifically do so with a modern language, which could replace C/C++ over time and has a focus on both performance and productivity, could bring a very valuable tool to any developer's toolbox.

Rust has actually been the most loved language every single year, from the 2016 survey, all the way to this year's 2020 survey.

Rust Overview

Rust is a C-style language, similar to C# or C++, and even has influences from C# (among others), with a focus on memory safety and performance.

The slogan on Rust's official website is:

A language empowering everyone to build reliable and efficient software.

The language is statically typed and is primarily imperative but also has functional aspects, similar to C#. It's an open-source language initially developed by Mozilla.

Wikipedia describes the Rust programming language in the following way:

Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++ but provides memory safety without using garbage collection.

As a C#-developer, you'll find the syntax familiar, with the "Hello World!" for Rust looking like this:

fn main() {
    println!("Hello World!");
}

Rust was announced in 2010, with version 1.0 released in 2015. In comparison, C# was released in the year 2000, Java in 1995, C++ in 1985, and C in 1972. So it's quite a modern language, which has been influenced by many other languages and has hopefully used many lessons learned from these.

Performance

Rust can be as fast as C and C++ and in some cases, it might be even faster. The code compiles to machine code, instead of compiling to an intermediate language (IL), like with C# or being interpreted on the fly, like with JavaScript or Ruby.

The memory management is not provided by using automated garbage collection (like in .NET/Java) or automatic reference counting (like in Swift/Objective C). The memory safety-guarantees are provided by Rust's key concept called Ownership. By enforcing Resource Acquisition Is Initialization (RAII), Rust makes sure that any objects which go out of scope get deconstructed and its resources are freed up.

Systems & Application Programming Language

Systems programming languages typically are more close to the metal and have more direct access to the physical hardware of the machine it is running on, which enables writing performant software, such as operating systems, drivers, embedded systems, game-engines, network services, and more. Assembly language, C, and C++ are examples of traditional systems programming languages.

C# is considered as an application programming language, which usually prioritizes productive ways of building software which gives functionality to end-users. They are usually not intended to compete on performance with systems programming languages.

Rust is a language that tries to straddle both systems and application programming.

Industry Adoption

The 2020 Stack Overflow Developer Survey shows that when it comes to the most professionally used programming language, C# has a 32,3% popularity, while Rust only has 4,8%. Comparing these numbers to JavaScript's 69,7% popularity might indicate that Rust is in an early phase of its widespread professional adoption.

The adoption of Rust does seem to be ramping up though, for good reasons. In a 2020 virtual talk about Rust at Microsoft, one of their cloud developer advocates said:

Rust is the industry’s best chance at safe systems programming

It does seem like Microsoft is exploring gradually switching to Rust from C and C++. These languages are not memory safe, which has caused a lot of costly errors, as mentioned before, with Microsoft's having around 70% of all their security bugs related to memory safety issues.

In 2019, Microsoft experimented with rewriting a low-level component in Windows with Rust, with a reported generally positive experience.

It's said that Amazon Web Services (AWS) uses Rust to power the deployment of the Lambda serverless runtime and some parts of EC2.

Many other companies are adopting Rust, including big players like Facebook (also with Libra), Apple, Google (with Fuchsia), and Cloudflare.

"From C# to Rust"-Series

Given that I'm learning Rust as I'm going, the goals of this series might change over time. But at this time, some topics I aim to cover are:

  • Fundamentals: Tools and concepts we need to understand
  • Code basics: Learning the basics of Rust-code and how the concepts relate to C#
  • Advanced code: Diving deeper into more advanced code and concepts in Rust
  • Rust specific functionality: Functionality and concepts which exists in Rust, but not in C#
  • Rust's drawbacks: Exploring what the reasons are behind the perceived steeper learning curve of Rust

There will probably be more subjects discovered along the way. Feel free to write in the comments if there are any specific subjects you're interested in, especially coming in as a C#-developer.

Summary

Some points that make me quite excited about Rust and motivated me to start this exploration are:

  • Modern C-style language, like C#, should give familiarity
  • The most loved language for half a decade
  • Enables both high-level productivity and low-level performance
  • Growing industry adoption, even by many of the giants
💖 💪 🙅 🚩
sebnilsson
Seb Nilsson

Posted on July 6, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

From C# to Rust: Code Basics
csharp From C# to Rust: Code Basics

July 28, 2020

From C# to Rust: Fundamentals
csharp From C# to Rust: Fundamentals

July 16, 2020

From C# to Rust: Introduction
csharp From C# to Rust: Introduction

July 6, 2020