From C# to Rust: Fundamentals

From C# to Rust: Fundamentals

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

Since this series 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. This is usually not done by Rust-developers, but instead, it's done through the Cargo-tool.

.NET equivalent: MSBuild / The C#-compiler (csc.exe)

Cargo: Build System & Package Manager

Cargo is used by Rust-developers to manage, build, test, and document their projects. This is done by using various cargo-commands.

.NET equivalent: .NET CLI and NuGet

Cargo-Commands

Cargo can be used to create a new project by running, for example, cargo new example_project.

.NET equivalent: dotnet new

As mentioned above, Cargo builds the project 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, 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 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, we use rustup, the Rust toolchain installer, which installs rustc, cargo, the rustup-CLI, and other Rust-tools.

.NET equivalent: .NET Core SDK

With Rustup, 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, or the C++ Build Tools and the Windows 10 SDK separately. Details on this can be found on Rustup's GitHub-page.

Package Manifest

When you run cargo new, your new project will get a Cargo.toml-file, 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 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 is used.

.NET equivalent: NuGet.org

Formatting Rust-code

Cargo contains a command which can automatically format all the Rust-code in your project. It's called rustfmt and can be executed by running cargo fmt.

Customizing the rules applied when formatting 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, which allows you to run dotnet format on your project.

Testing

Running the tests in a project is also done through Cargo, by running cargo test.

.NET equivalent: dotnet test

Testing is built into Rust, so you do not need to add any external testing-package.

VS Code Setup

First, make sure you've installed Rustup. 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, with the Rust-extension and the CodeLLDB-extension.

Consider evaluating the extension rust-analyzer as an alternative to the Rust-extension. It's not as well documented, but it seems to have more features, including the showing of type inference.

Next, create a new Rust-project by running cargo new. Now, all you have to do now is press F5 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, we get access to the complete Rust-toolchain. This includes, among other things, 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.

Comments