Now that Rust is installed, you can write your first Rust program after creating a project directory.
- [Instructor] Now that we have Rust installed let's write our first Rust program. It's tradition when learning on any new language to write a little program to print the text "hello world" to the screen. So let's get started. We'll make a project directory where we can store all our files in one place. Let's open the Windows PowerShell and make our directories here. Use the command "mkdir" to make a new folder. So the name of the folder here is "sample." On the terminal, you can use the cd command to navigate into the directory.
In order to check what directory we are in, use the pwd command. Let's clear up the screen for you. If you'd like to list the files present in this directory, just type "ls." For now, the directory we've created is empty. Let's jump to our Visual Studio Code editor and make a new file here. File, new file, and control-S to save it.
In Rust, the source code files always end with the extension ".rs." If you have two words in your file name, it's always nice to separate it with an underscore. Without further ado, let's jump into a Rust code. Let me type the first line, which starts with "fn," which stands for function, and "main," which is the name of a function followed by the brackets where we can pass parameters to this function if we'd like.
Always remember that your function start with a curly brace and ends with a curly brace. In Rust, it's important that we indent our code and indentation here is four spaces. The main function is special because that's the first thing that runs for any executable Rust program. Now let's go ahead and print out some text on screen. For this, I will be using the "println" macro. In Rust, if the functions end with the exclamation point that means that it's a macro.
We'll see what macro's in a later video. So for this macro, I'm gonna pass "Hello, world!" as the string and make sure that you end your line with a semicolon. In Rust, most statements end with a semicolon, but all of them don't need to. So let's just go ahead and save this file. It's important to note that Rust is an ahead-of-time compile language. That means that you have to compile your source code first which then generates a binary executable and then, is supposed to run the executable that's generated for you.
In order to run the file we just said, use "rustc" and "hello_world.rs." So if I hit enter now, it generates an executable. Like I said previously, we can use the ls command to see the files that are generated for us. So as you can see, the "hello_world.exe" is generated for us. Now all that's left to do, is to run this file. So if you can see "hello world" on your screen, job well done.
You've successfully compiled and run your first Rust program.
- Installing Rust
- Data types
- Adjusting memory allocation
- Setting up race conditions
- Working with functions
- Exploring references and borrowing
- Using structs
- Method syntax
- Demonstrating recoverable errors with Result
- Working with enums
- Getting user input