Cargo

3 minutes
Share the link to this page
Copied
  Completed

Give a rundown of Cargo and how to use it.

In this video:

  • Learn what Cargo is capable of
  • Use Cargo to create, compile, and run a project

Transcript

Cargo is fantastic. It's the first tool that you will use and you'll keep using it for all sorts of things. Cargo is a package manager. Wait a package manager for a viable systems programming language. Yes. Finally, let that sink in systems programmers can have nice things to you use cargo to search for install and manage packages that you want to use.

Cargo is also the build system. No more make files. Cargo is also the test runner, and the documentation generator, etc. It's like all of the good parts have NPM and PIP and bundler and make, let's do something real. If you've installed rust, you can open up your terminal right now and follow along. If you type cargo new hello and hit enter cargo will happily and colorfully create a project named Hello for you.

Let's take a look at what Pargo created for us a Hello directory with a config file named cargo Thomas and a source subdirectory with a main.rs file. config files in rust use the tunnel format which stands for Tom's obvious minimal language rust source files use the.rs extension as you see on main.rs here now launch your favorite editor that you set up and prepared with rust support and open up cargo tamo cargo Tama is the config file for your project. In fact, it is the authoritative source of information about your project name is the actual name of your project. It doesn't matter what the name of the directory is, or what the name of your get repository is. This setting determines the name of your project rust uses semantic versioning which means a version number is always three numbers separated by dots and each number has a specific meaning.

If you aren't familiar with semantic versioning already, please see semver.org for more details because I'm not going to go into it any further cargo will serve Bunch of likely places for your name and email address. If it's able to find it, it will fill it out for you. One of the places that searches is your global get config edition should be 2018. If cargo didn't add this line for you automatically, then you're using an older version of rust and you need to update it now by running rust up update. Unless of course, it's a higher year, in which case you're far in the future. And you can ignore that maybe you should look for an updated version of this course Later I will show you how to manage your dependencies here.

But that's all we need to know about cargo Tamil for the moment. Now let's open up main de RS in the source directory. It already has a HelloWorld program for us. That's fantastic. What a helpful language. Let's go back to our terminal cargo run is the command to build and run your project in one step.

If you run your project now, you will see hello world right after cargo finishes building. If you run it again, you will see that cargo can tell that nothing changed in the source code. So it doesn't recompile the project notice what cargo is running target debug Hello, let's take a look. There's a target directory where cargo outputs all of its build artifacts. This is definitely a directory you want your version control software to ignore. We can run the binary directly if we want.

Did you notice that it's in a debug subdirectory cargo compiles your project with debug symbols by default, add dash dash release to your cargo run command to compile without debug symbols, it's a lot faster this way. You'll notice a similar result only this time it will be stored in the release sub directory. Most code will run significantly faster in release mode, but it takes a little longer to compile. I suggest you keep this Hello, project around and use it for little experiments. In the next video, we will talk about variables

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.