First Steps with Cargo
To start a new package with Cargo, use cargo new:
1. $ cargo new hello_world
Cargo defaults to —bin to make a binary program. To make a library, we'dpass —lib.
Let’s check out what Cargo has generated for us:
1. $ cd hello_world
2. $ tree .
3. .
4. ├── Cargo.toml
5. └── src
6. └── main.rs
8. 1 directory, 2 files
This is all we need to get started. First, let’s check out Cargo.toml:
1. [package]
2. name = "hello_world"
3. version = "0.1.0"
4. authors = ["Your Name <you@example.com>"]
5. edition = "2018"
7. [dependencies]
This is called a manifest, and it contains all of the metadata that Cargoneeds to compile your package.
Here’s what’s in src/main.rs:
1. ```
fn main() {
```
2. ```
println!("Hello, world!");
```
3. ```
}
```
Cargo generated a “hello world” for us. Let’s compile it:
1. $ cargo build
2. Compiling hello_world v0.1.0 (file:///path/to/package/hello_world)
And then run it:
1. $ ./target/debug/hello_world
2. Hello, world!
We can also use cargo run to compile and then run it, all in one step:
1. $ cargo run
2. Fresh hello_world v0.1.0 (file:///path/to/package/hello_world)
3. Running `target/hello_world`
4. Hello, world!
Going further
For more details on using Cargo, check out the Cargo Guide
