Running Code Locally with Cargo

If you want to experiment with the code on your own system, then you will need to first install Rust. Do this by following the instructions in the Rust Book. This should give you a working rustc and cargo. At the time of writing, the latest stable Rust release has these version numbers:

  1. % rustc --version
  2. rustc 1.69.0 (84c898d65 2023-04-16)
  3. % cargo --version
  4. cargo 1.69.0 (6e9a83356 2023-04-12)

You can use any later version too since Rust maintains backwards compatibility.

With this in place, follow these steps to build a Rust binary from one of the examples in this training:

  1. Click the “Copy to clipboard” button on the example you want to copy.

  2. Use cargo new exercise to create a new exercise/ directory for your code:

    1. $ cargo new exercise
    2. Created binary (application) `exercise` package
  3. Navigate into exercise/ and use cargo run to build and run your binary:

    1. $ cd exercise
    2. $ cargo run
    3. Compiling exercise v0.1.0 (/home/mgeisler/tmp/exercise)
    4. Finished dev [unoptimized + debuginfo] target(s) in 0.75s
    5. Running `target/debug/exercise`
    6. Hello, world!
  4. Replace the boiler-plate code in src/main.rs with your own code. For example, using the example on the previous page, make src/main.rs look like

    1. fn main() {
    2.     println!("Edit me!");
    3. }
  5. Use cargo run to build and run your updated binary:

    1. $ cargo run
    2. Compiling exercise v0.1.0 (/home/mgeisler/tmp/exercise)
    3. Finished dev [unoptimized + debuginfo] target(s) in 0.24s
    4. Running `target/debug/exercise`
    5. Edit me!
  6. Use cargo check to quickly check your project for errors, use cargo build to compile it without running it. You will find the output in target/debug/ for a normal debug build. Use cargo build --release to produce an optimized release build in target/release/.

  7. You can add dependencies for your project by editing Cargo.toml. When you run cargo commands, it will automatically download and compile missing dependencies for you.

Try to encourage the class participants to install Cargo and use a local editor. It will make their life easier since they will have a normal development environment.