Pure Programmer
Blue Matrix


Cluster Map

Dependency Management

L1

This page is under construction. Please come back later.

It is often easier to write programs when you don't have to write all the code from scratch. There are a large number of code libraries available, both open source (free) and commercial (paid). Using code libraries written by the community or commercial developers give you a leg up in your coding efforts by making a large library of useful functions and classes available to your program. All we need to do is manage these dependencies and include them in our programs.

Rust organizes code in to modules called "crates". All you need to do to use pre-written module is to download the .rs Rust module file, put it in your project folder and then include it in your program.

An easier way is to use the [[Cargo]] software that comes with Rust to download and install libraries for you. The community maintains a large online library of crates that you can access using cargo. Given the name of a module such as regex and a version, you can add it to your cargo project configuration file "Cargo.toml" in one step from the command line:

$ cargo add regex@1.9.4

Once added to your "Cargo.toml" file, the crate will be downloaded and installed next time you build and run your project with cargo.

You will want to download the [[Pure Programmer Rust Utils Crate]] which is needed by some examples and project. Simply put the "utils.rs" file in the same folder as your program. Then add a mod or use statement to the beginning of your source file depending on whether the crate is a source file in your directory like "utils.rs" or a crate downloaded and installed by cargo:

mod utils;
use regex::Regex
Using Rust Crates

The entire process of setting up a cargo project takes just a few commands. The basic steps are:

  1. Create cargo empty project directory and files
  2. Create "bin" directory in "src" directory
  3. Add source files and "utils.rs" to "bin" directory
  4. Add dependent crates using cargo
  5. Build cargo
  6. Run executable
$ mkdir ~/Desktop/projects $ cd ~/Desktop/projects $ cargo new rust_projects $ cd rust_projects $ mkdir src/bin $ cp ~/Downloads/HelloWorld.rs src/bin $ cp ~/Downloads/utils.rs src/bin $ cargo add fstrings@0.2.3 Updating crates.io index Adding fstrings v0.2.3 to dependencies. Features: - nightly - verbose-expansions Updating crates.io index $ cargo add regex@1.9.4 Updating crates.io index Adding regex v1.9.4 to dependencies. Features: + perf + perf-backtrack + perf-cache + perf-dfa + perf-inline + perf-literal + perf-onepass + std + unicode + unicode-age + unicode-bool + unicode-case + unicode-gencat + unicode-perl + unicode-script + unicode-segment - logging - pattern - perf-dfa-full - unstable - use_std Updating crates.io index $ cargo build --bin HelloWorld Compiling proc-macro2 v1.0.66 Compiling unicode-ident v1.0.11 Compiling syn v1.0.109 Compiling proc-macro-hack v0.5.20+deprecated Compiling memchr v2.6.3 Compiling regex-syntax v0.7.5 Compiling aho-corasick v1.0.5 Compiling quote v1.0.33 Compiling regex-automata v0.3.8 Compiling regex v1.9.5 Compiling fstrings-proc-macro v0.2.3 Compiling fstrings v0.2.3 Compiling rust_projects v0.1.0 (/Users/rich/Desktop/projects/rust_projects) $ target/debug/HelloWorld Hello, world!

Once setup you can use this project for all the examples and projects on this website. Just download source files and put them in the "src/bin" directory of the project. Be sure to use the name of the source file when you build and run the program.

If your Rust program doesn't use any crates or "utils.rs" you can simply use the Rust compiler rustc to build your source file into an executable.

HelloWorld.rs
/******************************************************************************
 * Simple first program.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

fn main() {
	println!("Hello, world!");
}

Output
$ rustc HelloWorld.rs $ ./HelloWorld Hello, world!

References