CARGO: Rust's Package Manager (Think npm or pip)
Building a CARGO Project In the last article we learnt how we create a new CARGO project using the cargo new command in the command line. Now it’s time to build that project. We will have a main.rs in the /src folder that was created when we executed the cargo new command. It contains code for basic ‘Hello, World!’ program that we build in the last article. Let us use that as a base for now, we…
Building a Cargo Project
After writing the 'Hello, World!' code in a human-readable format, the next step is to convert it into a machine-understandable code and an executable to run directly. This process is called 'building' the project. To build a Cargo project, run the command 'cargo build' in the newly created folder from the 'cargo new' command. This creates an executable file in the './target/debug' folder with the same name as the project.
The first time running 'cargo build' also generates a 'Cargo.lock' file, which keeps track of the exact versions of dependencies in the project. To execute the program, navigate to the executable in the terminal; for instance, in the example given, the command './target/debug/cargo_intro' will display 'Hello, world!'. However, this method can be cumbersome if repeated frequently.
Cargo simplifies this process by allowing users to run the executable directly with the 'cargo run' command. If any files are modified, 'cargo run' automatically rebuilds and runs the executable, eliminating the need for separate 'cargo build' and execution steps. For example, changing 'Hello, World!' to 'Hello, my name is Aditya.' will prompt a rebuild when running 'cargo run'.
Additionally, the 'cargo check' command allows users to verify if the code compiles without generating an executable.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.