From C# to Rust: Fundamentals
Tags: csharp, dotnet, rust
Published: 2020-07-16 13:44:15

---
title: From C# to Rust: Fundamentals
description: "Rust has some fundamental tools and concepts we need to understand before we get into the code."
date: 2020-07-16 13:41:00 UTC
published: true
tags: csharp, dotnet, rust
canonical_url: https://sebnilsson.com/blog/from-csharp-to-rust-fundamentals/
cover_image: https://files.sebnilsson.com/web/images/rust/rust-cover-img-bw.jpg
series: "From C# to Rust"
---

There are some **fundamental tools and concepts which we should understand** before we get into actually coding Rust.

Since [this series](https://dev.to/sebnilsson/from-c-to-rust-introduction-4650) focuses on Rust from **the perspective of a C#-developer**, we will try to **relate the Rust-concepts to the equivalent concepts in C#** and .NET.

# Modern Toolchain

Rust comes with a complete toolchain of **everything you need to compile and run your Rust-code**. All the way from the compiler, to package-management, testing, code-formatting, linting, documentation-generation, and more.

# The Rust Compiler

The compiler for Rust **can** be executed by using the command [`rustc`](https://doc.rust-lang.org/rustc/what-is-rustc.html). **This is usually not done** by Rust-developers, but instead, it's done through the Cargo-tool.

**.NET equivalent**: [MSBuild](https://docs.microsoft.com/visualstudio/msbuild/msbuild) / [The C#-compiler (csc.exe)](https://docs.microsoft.com/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe)

# Cargo: Build System & Package Manager

[Cargo](https://doc.rust-lang.org/cargo/) is used by Rust-developers to manage, build, test, and document their projects. This is done by using various [cargo-commands](https://doc.rust-lang.org/stable/cargo/commands/).

**.NET equivalent**: [.NET CLI](https://docs.microsoft.com/dotnet/core/tools/) and [NuGet](https://docs.microsoft.com/nuget/)

## Cargo-Commands

Cargo can be used to [create a new project](https://doc.rust-lang.org/cargo/guide/creating-a-new-project.html) by running, for example, `cargo new example_project`.

**.NET equivalent**: `dotnet new`

As mentioned above, Cargo [builds the project](https://doc.rust-lang.org/cargo/guide/working-on-an-existing-project.html) and this is done by running the command `cargo build`.

**.NET equivalent**: `dotnet build`

Cargo is the Rust-equivalent of .NET's NuGet, so it's also used to [resolve and download libraries](https://doc.rust-lang.org/cargo/guide/dependencies.html), which it does automatically when you run the `cargo build`-command.

Now that your code has been built, you can run the code, given that it's a binary. This is done by using `cargo run`.

**.NET equivalent**: `dotnet run`

To just [check that the code is valid](https://doc.rust-lang.org/edition-guide/rust-2018/cargo-and-crates-io/cargo-check-for-faster-checking.html) and if that it can build, but not actually do a full build, there is a more lightweight command in Rust, which is `cargo check`.

# Rustup: Rust Toolchain Installer

To easily get access to the [complete toolchain](https://stackoverflow.com/a/62419829/2429), we use [`rustup`, the Rust toolchain installer](https://rustup.rs/), which installs `rustc`, `cargo`, the [`rustup`-CLI](https://github.com/rust-lang/rustup), and other Rust-tools.

**.NET equivalent**: .NET Core SDK

[With Rustup](https://doc.rust-lang.org/edition-guide/rust-2018/rustup-for-managing-rust-versions.html), you can change which version of Rust is used, install different platforms for cross-compiling, and installing various other components.

## Rust on Windows

**For Rust and Rustup to work on Windows**, you need to [install Visual Studio](https://visualstudio.microsoft.com/downloads/), or the [C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) and the [Windows 10 SDK](https://developer.microsoft.com/windows/downloads/windows-10-sdk) separately. Details on this can be found on [Rustup's GitHub-page](https://github.com/rust-lang/rustup#working-with-rust-on-windows).

# Package Manifest

When you run `cargo new`, your new project will get a [`Cargo.toml`-file](https://doc.rust-lang.org/cargo/reference/manifest.html), which is the manifest that contains the meta-data about your project. 

An example-version of the file, with the **added dependency to the _regex_-package**, looks like this:

```
[package]
name = "example_project"
version = "0.1.0"
authors = ["Seb Nilsson"]
edition = "2018"

[dependencies]
regex = "1.3.9"
```

**.NET equivalent**: The `.csproj`-file (or the formerly used `packages.config`-file).

**Node.js equivalent**: The `package.json`-file.

# Crates

In Rust, the term [_crate_](https://doc.rust-lang.org/book/ch07-01-packages-and-crates.html) is used for a package. It can be either an executable binary or a library.

**.NET equivalent**: NuGet-packages

## Crates.io

To find useful packages/crates to include in your Rust-projects, the registry-site [Crates.io](https://crates.io/) is used.

**.NET equivalent**: [NuGet.org](http://nuget.org/)

# Formatting Rust-code

Cargo contains a command which can automatically format all the Rust-code in your project. It's called [_rustfmt_](https://github.com/rust-lang/rustfmt) and can be executed by running `cargo fmt`.

[Customizing the rules applied when formatting](https://rust-lang.github.io/rustfmt/) is done by adding a `rustfmt.toml`-file to the project, which _rustfmt_ then will follow.

**.NET equivalent**: There is no built-in way to do this in the .NET CLI, but there is .NET Global Tool from the .NET-team called [_dotnet-format_](https://github.com/dotnet/format), which allows you to run `dotnet format` on your project.

# Testing

[Running the tests in a project](https://doc.rust-lang.org/cargo/guide/tests.html) is also done through Cargo, by running `cargo test`.

**.NET equivalent**: `dotnet test`

[Testing is built into Rust](https://doc.rust-lang.org/book/ch11-00-testing.html), so you do not need to add any external testing-package.

# VS Code Setup

First, make sure you've [installed Rustup](https://rustup.rs/). You can verify that you have it installed by running the command `rustup --version`, which should show you a version-number.

To get started developing in Rust, with features such as syntax highlighting, auto-complete, code-formatting, refactoring, debugging, and much more, you can install [Visual Studio Code](https://code.visualstudio.com/), with the [_Rust_](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust)-extension and the [_CodeLLDB_](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)-extension.

Next, create a new Rust-project by running `cargo new`. Now, all you have to do now is press <kbd>F5</kbd> in VS Code and it will automatically create a `launch.json` for you, which allows you to debug your Rust-code.

# Summary

By just installing [`rustup`](https://rustup.rs/), we get access to the complete Rust-toolchain. This includes, among other things, [Cargo](https://doc.rust-lang.org/cargo/), which lets us **work through the full development-cycle** with our projects, like: **create new** projects, **compile** the code, **run the code**, **format** the code, and **run the tests** in our projects.

Add VS Code with a few Rust-extensions, and you'll have a great developer-experience to get started with.