Adding dependencies

Let's create a new project with all the necessary dependencies. We will create a binary utility for managing users in a database. Create a new binary crate:

cargo new --bin users

Next, add the dependencies:

cargo add clap postgres

But wait! Cargo doesn't contain an add command. I've installed cargo-edit tool for managing dependencies. You can do this with the following command:

cargo install cargo-edit

The preceding command installs the cargo-edit tool. If you don't install it, your local cargo won't have an add command. Install the cargo-edit tool and add the postgres dependency. You can also add dependencies manually by editing the Cargo.toml file, but as we are going to create more complex projects, the cargo-edit tool can be used to save us time.

The Cargo tool can be found here: https://github.com/killercup/cargo-edit. This tool contains three useful commands to manage dependencies: add to add a dependency, rm to remove an unnecessary dependency, and upgrade to upgrade versions of dependencies to their latest versions. Furthermore, with the awesome Edition 2018 of Rust, you don't need to use an extern crate ... declaration. You can simply add or remove any crates and all of them will be available immediately in every module. But what about if you add a crate that you don't need, and end up forgetting about it? Since the Rust compiler allows unused crates, you can add the following crate-wide attribute, #![deny(unused_extern_crates)], to your crate. This is necessary in case you accidentally add a crate that you won't use.

Also, add the clap crate. We need it for parsing arguments for our tool. Add the usages of all the necessary types, as follows:

extern crate clap;
extern crate postgres;

use clap::{
crate_authors, crate_description, crate_name, crate_version,
App, AppSettings, Arg, SubCommand,
};
use postgres::{Connection, Error, TlsMode};

All necessary dependencies have been installed, and our types have been imported, so we can create the first connection to a database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.136