The structure of new Rust projects
Inside a newly created Rust project, you’ll find three things:
- A
src
folder, where your Rust code (Rust files end in.rs
) lives - A
.gitignore
file, because version control thinking is built-in - A
Cargo.toml
file, which is the manifest file. This is the project configuration and dependencies script. This would be like theGemfile
in Ruby, orpackage.json
in Node.
Anytime we create a new project like this, Rust sticks in some default code for
us. Open up src/main.rs
and let’s see what we can intuit about Rust’s syntax:
|
|
There’s so much we can infer from this tiny block of code:
- Functions are declared with
fn
; - the
printf
equivalent isprintln!
, which is a macro and not a function; - this program does nothing more than print
"Hello, world!"
to the command line.
Before we get ahead of ourselves, let’s build this project. Open up your
terminal (if you’re using VS Code, you can use the integrated terminal by typing
Ctrl
+`
), and then type cargo build -q
:
$ cargo build -q
The build
command compiles your project and builds your executable. (This will be
the last time I mention that the -q
flag tells cargo to “be quiet” and not emit
anything it doesn’t need to).
The executable is a “development” version. The main difference between a development and release version is that in a development version the code is not optimized and there are debug symbols (to help us track down issues) present that we would normally omit from a release version.
When Cargo is finished building your project, you’ll have a new folder
named target
in your project directory. Inside that folder is where
you’ll find your executable.
We have covered two Cargo commands so far:
cargo new
, to create a new Rust project, andcargo build
, to build a Rust project.
The third Cargo command we will often use is cargo run
, which is how we
will run the executable that our project builds.
Let’s try running our current project that we just built:
$ cargo run -q
Hello, world!
Note that we don’t need to pass the name of the project; Rust will infer that for us, by executing the appropriate binary based on the release target.
If you see “Hello, world!” in your terminal, then congratulations: you have built your first Rust program!