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.
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 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.
Cargo can be used to create a new project by running, for example,
cargo new example_project.
As mentioned above, Cargo builds the project and this is done by running the command
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
Now that your code has been built, you can run the code, given that it's a binary. This is done by using
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
.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.
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
Node.js equivalent: The
In Rust, the term crate is used for a package. It can be either an executable binary or a library.
.NET equivalent: NuGet-packages
To find useful packages/crates to include in your Rust-projects, the registry-site Crates.io is used.
.NET equivalent: NuGet.org
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
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.
Running the tests in a project is also done through Cargo, by running
Testing is built into Rust, so you do not need to add any external testing-package.
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.
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.
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.