All the dependencies required by the Rust language can be managed by cargo
which also acts as the build tool. If any dependency is missing from the project, cargo
will automatically download the package and build the library for the project.
Same as git
, to create an empty project that can be managed by cargo
, one only needs to enter the following command:
$ cargo new "some_project" # or cargo new from the current directory.
$ cd ./some_project
In addition, a typical cargo
-created project will contain the following files and directories:
.
├── Cargo.lock
├── Cargo.toml
├── src
└── target
Cargo.toml
: This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
// Some dependencies
The first line, [package]
, is a section heading that indicates that the following statements are configuring a package. As we add more information to this file, we’ll add other sections. The last line, [dependencies]
, is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.
Cargo.lock
contains exact information about your dependencies. It is maintained by Cargo and should NOT be manually edited.
Source code goes in the src
directory.
The default executable file is src/main.rs
.
src/bin/
.Benchmarks go in the benches
directory.
Examples go in the examples
directory.
Integration tests go in the tests
directory.
You could build the project by
$ cargo build
cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
You could run the project by
$ cargo run
cargo run
Compiling ch1_hello_rust v0.1.0 (/home/hiroki/rust-learning/ch1_hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.56s
Running `target/debug/ch1_hello_rust` # This is where the targetl locates.
Hello, world!
cargo check
.