Where we use cargo to setup a Rust project
The following needs to be prepared
- Terminal with access to
cargo
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.