Cargo Beginner
Working onwards from page 8 in the book 'The Rust Programming Language (2nd Edition)' I begin to learn about Cargo which is the build system and package manager for the Rust programming language.
First I check I have cargo installed along side rust by typing in the following command:
cargo --version
Gives the following output at the time of writing:
cargo 1.87.0 (99624be96 2025-05-06)
One thing I really like about Cargo and that it comes with rust when you install it via Rustup, is you can use it to initiate and setup a new project very easily I find, compared to a language like C++ which I have used in the past as follows:
cargo new hello_cargo
this then sets up everything I need in the project as follows:
- a .git folder ready for git
- a Cargo.toml file for configuration of the project
- a .gitignore file although seems to be mostly blank?
- a src/main.rs file and folder with the main starting code, seems to have added a simple hello world program in here.
You can then compile the program in there hello world again but this way using cargo, but first lets check it compiles by doing the following:
cargo check
then to compile the project to debug I do following
cargo build
then to run the project I do the following
cargo run
then like before, but finding the executable in the relevant folder I can run the executable / binary file by doing something like ./main
if I want to build for release rather than the default debug then I do the following instead:
cargo build --release
Release make the Rust code run faster but compile slower, whilst debug runs slower but compiles faster overall.
One funny thing I realised with this is I made a rust learning folder for this book, obviously then adding .git folder to it at the start, forgetting then that making a rust project within creates another .git folder messing that initial .git folder up, just something I need to remember going forwards. Sometimes the system can hide the .git folder so on mac you can do command ls -a
to check how many .git folders there are in a project as you only need one relevant to your repo I think.
Further to which I found out you can do the following:
cargo new hello_cargo --vcs none
This then only creates a src/main.rs
file and a Cargo.toml
which for myself going forwards is ideal if I am working through exercises thus have many different project folders with a .git
folder at the root overall.
Title: Cargo Beginner
Author: Jamie Cropley
Date published: 16/05/2025
URL: https://www.rust.foo/blog/cargobeginner/
Accessed on: 17/05/2025
Website: Rust Foo