From C# to Rust: Introduction
Tags: csharp, dotnet, rust
Published: 2020-07-06 14:06:10

---
title: From C# to Rust: Introduction
description: "Rust has been the most loved programming language for the last 5 years. For a C#-developer, it could be a powerful addition to the tool-belt."
date: 2020-07-06 13:49:00 UTC
published: true
tags: csharp, dotnet, rust
canonical_url: https://sebnilsson.com/blog/from-csharp-to-rust-introduction/
cover_image: https://files.sebnilsson.com/web/images/rust/rust-cover-img.jpg
series: "From C# to Rust"
---

[Rust](https://www.rust-lang.org/) has been the **[most loved programming language](https://stackoverflow.blog/2020/01/20/what-is-rust-and-why-is-it-so-popular/) 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](https://www.zdnet.com/article/microsoft-70-percent-of-all-security-bugs-are-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](https://insights.stackoverflow.com/survey/2016#technology-most-loved-dreaded-and-wanted), all the way to this year's [2020 survey](https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-loved).

# Rust Overview

Rust is a C-style language, similar to C# or C++, and even has [influences from C#](https://doc.rust-lang.org/reference/influences.html) (among others), with a focus on **memory safety and performance**.

The slogan on [Rust's official website](https://www.rust-lang.org/) is:

> A language empowering everyone to build reliable and efficient software.

The language is [statically typed](https://en.wikipedia.org/wiki/Static_typing) and is primarily [imperative](https://en.wikipedia.org/wiki/Imperative_programming) but also has [functional](https://en.wikipedia.org/wiki/Functional_programming) aspects, **similar to C#**. It's an open-source language [initially developed by Mozilla](https://research.mozilla.org/rust/).

Wikipedia describes the [Rust programming language](https://en.wikipedia.org/wiki/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](https://doc.rust-lang.org/reference/influences.html) 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](https://rustc-dev-guide.rust-lang.org/overview.html), instead of compiling to an [intermediate language (IL), like with C#](https://docs.microsoft.com/en-us/dotnet/standard/managed-code) or being [interpreted on the fly](https://en.wikipedia.org/wiki/Interpreted_language), 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](https://doc.rust-lang.org/1.8.0/book/ownership.html)_**. By enforcing [Resource Acquisition Is Initialization (RAII)](https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization), 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](https://doc.rust-lang.org/book/foreword.html) systems and application programming**.

# Industry Adoption

The 2020 _[Stack Overflow Developer Survey](https://insights.stackoverflow.com/survey/2020)_ shows that when it comes to **the most professionally used programming language**, [C# has a 32,3% popularity, while Rust only has 4,8%](https://insights.stackoverflow.com/survey/2020#most-popular-technologies). 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](https://youtu.be/NQBVUjdkLAA?t=580), 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](https://thenewstack.io/microsoft-rust-is-the-industrys-best-chance-at-safe-systems-programming/) 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](https://www.zdnet.com/article/microsofts-rust-experiments-are-going-well-but-some-features-are-missing/), 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](https://www.rust-lang.org/production/users), including big players like [Facebook](https://www.youtube.com/watch?v=kylqq8pEgRs) (also with [Libra](https://developers.libra.org/docs/community/coding-guidelines)), [Apple](https://twitter.com/benwilliamson/status/1240113606374686721), Google (with [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia)), and [Cloudflare](https://blog.cloudflare.com/tag/rust/).

# _"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**](https://dev.to/sebnilsson/from-c-to-rust-fundamentals-4cgm): 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