From the course: Rust for C# Developers

Modern toolchain: Running Rust code - Rust Tutorial

From the course: Rust for C# Developers

Modern toolchain: Running Rust code

- With Rust installed via the tool rustup, you will now have access to the full modern tool chain for Rust. This enables you to create a new Rust package, compile the code, run the tests in the code, execute the code and more. The main executable use that you got installed through rustup is called Cargo. This is Rust's package manager, but also much more. Even if Cargo calls itself Rust's package manager, it is much more than that. The CLI commands that Cargo exposes include functionality such as Cargo New to create a new package, Cargo Build to compile Rust Code, Cargo Run to run Rust code and much more. This makes Cargo the equivalent of the .NET CLI using the .Net, D-O-T-N-E-T in the command line for .Net more than just a.Net package manager, Newgate. So let's walk through a very basic workflow while setting up a project or a package as it's called in Rust and working with it in a basic way. This will show that rustup, the modern tool chain does provide functionality for all the basic steps when developing with Rust. Let's start by setting up a new Rust package which we will work with to get familiar with Rust and the workflow its tool chain provides. In your favorite command line, execute the command "cargo new rust dash from dash csharp." (keyboard hitting sound) This is the equivalent of running the .Net CLI command ".Net new dash dash name project name." (keyboard typing sound) This will create a new folder called Rust dash from dash csharp, which includes the file Cargo.toml and the sub folder src, which contains the file, main.rs. (keyboard typing sounds) This file is a starting point for the application so it's the equivalent of the program .cs in C#. The main.rs file is the entry point to our Rust application and the Cargo New command will have created the basic "Hello, World!" Rust code in this file. Open the main.rs file in your favorite text editor. In my case, I'm using Notepad. Don't worry about not having any tooling in the editor at this point. We will get to that later. In this code, change the generic string, "Hello, world!" into something more creative like, "Hello Rust, from the C# world!" This will allow us to ensure that our changes were applied correctly and are affecting the functionality of the Rust code. To verify that our changes did take effect and that our application is set up properly, we want to execute the Rust app and see that output generated is correct. You do so by executing the command line cargo run in your favorite command line. This is the equivalent of executing .Net run through the .Net CLI. (keyboard typing sound) What you should see is the Rust compiler compiling the code and then running it, giving the output text, "Hello Rust, from the C# world!" Now you've been exposed to the very basics of the development cycle of a Rust application. With the knowledge gathered in this video, you can actually start developing Rust applications right away. Just modify the code in the main.rs and use Cargo to run the code. But of course, there's a lot more to learn about Cargo and much better tooling available for working with Rust than what's included in this video where we're just covering the absolute basics.

Contents