Skip to content

Files

Latest commit

93069ba Β· Aug 24, 2015

History

History
67 lines (47 loc) Β· 1.64 KB

README.md

File metadata and controls

67 lines (47 loc) Β· 1.64 KB

episode-002

Where we use cargo to setup a Rust project

Setup

The following needs to be prepared

  • Terminal with access to cargo

Script

We can use rustc to compile Rust programs, but even for small projects this gets unwieldly. When we installed Rust another tool became available: cargo.

cargo is a jack of all trades for Rust, but in this episode we focus on building a Rust project.

By executing the command

cargo new hello --bin

a directory hello is created with the following structure

hello
β”œβ”€β”€ Cargo.toml
└── src
    └── main.rs

With a quick glance into main.rs we discover the familiar "Hello, World!" program. When we enter the just created directory hello and execute

cargo build

the project is build in debug-mode. We will talk about Cargo.toml and Cargo.lock in an other episode. For now we look into the target directory.

target
└── debug
    β”œβ”€β”€ build
    β”œβ”€β”€ deps
    β”œβ”€β”€ examples
    β”œβ”€β”€ hello
    β”œβ”€β”€ hello.dSYM
    β”‚   └── Contents
    β”‚       β”œβ”€β”€ Info.plist
    β”‚       └── Resources
    β”‚           └── DWARF
    β”‚               └── hello
    └── native

For now the most important part is the target/debug/hello executable. We could run it by calling it directly, but instead the cargo command can be used as well.

By issuing

cargo run

target/debug/hello is executed.

And there you have it, we used cargo to create and build a Rust project.