Rust has a powerful and flexible module system. Let's use this Hello project as an example, let's add a route library module by creating a lib.rs file. We already know that main.rs is a special file that will be the Hello binary lib.rs is a special file that will be the root of the Hello library. Let's open up lib.rs and put a function inside it. Here's a pretty simple function. It's the only contents of the library.
Let's go split screen and look at Main Rs. The main function attempts to call the library's greet function directly by specifying the absolute path to the function. The absolute path is the library name which is the same as the name of your project in cargo tamo Hello, in this case, then the scope operator which is double colons, and then the name of the function greet this almost works But all items in a library are private by default, even to binaries in the same project. Let's add pub in front of the greet function to make it public. Now the code works. But specifying the absolute path of something at every call site could be really painful, especially if the path is really long, which is where the use statement comes in use brings an item from some path into some scope.
And I will often call it import without thinking about it because I'm so used to Python. So let's go back to our example and add a use statement above the main function. Since the use statement is outside of any smaller scope, this brings greet into scope for all of Maine. Now we can simplify the function call to just greet. This works exactly the same as before, but it's more concise at the calling site. This use statement is how you will bring into scope anything from the standard library or from any other Another project that you want to use, the standard library is always available by default, it's spelled STD.
But when you read it out loud, you say standard. So this is standard collections hashmap, you will become really familiar with the contents of the standard library. But until you do, let me show you a shortcut for how to find documentation for things in the standard library. Go to Google and type in rust standard. And then the thing you want to find for example, if I wanted to know about the vector type, I would put rust standard vector, you will find that the top result is nearly always the page for the standard library item that you need. That's pretty cool.
I use it all the time still, but what if you need something that's not in the standard library crates.io is rusts package registry. That's where I would start for today's purposes. You can consider crate to be a synonym for package. Most of the rust community will use crate and package interchangeably with that in mind the name crates.io makes sense for a package registry. But since crate technically has multiple meanings, and I want to be precise in my teaching, I all mostly say package. Once you have identified the name and version of the package that you want to use, you need to go back to cargo Tama and add the package as a dependency.
We're going to do this in the dependencies section that we skipped earlier. The format is the name of the package, then an equal sign and then the version of the package in quotation marks. Here I've specified the RAND package to use for generating random numbers. Now that I have it listed in my dependencies. I can use it from both my library and my binary either by an absolute path or by bringing a specific item into scope with a use statement. In the next video, we will go over scalar types