INSTALLING CARGO
- Cargo is a Rust Package Manager.
- It downloads your Rust package's dependencies, compiles your package, makes distributable packages and upload them to cargo.io.
- Basically the same as Pip of Python, SBT of Scala and Pub of Dart.
- Highly recommend to create project with cargo, as it makes your life easier.
- You might ask why not run code using rustc directly?
- In Rust each library is called a crate, the initally generated hello world rust code is a crate. When using rustc you need to specifiy the crate name when running the command rustc main.rs. This is more complex when adding external crates or specifying compiler flag as the command to compile the code becomes larger.
- So in nutshell use Cargo.
- To create a new project called hello_world using cargo, use the below command:
cargo new hello_world
- There will be cargo.toml, it contains all the meta data needed by cargo to compile the packages.
- And then there is the src/main.rs which is where you will write your rust code.
- To run the rust code, use the below command:
cargo build
- And When you are ready to release, you can use below command:
cargo build --release
- And can run the compiled code in ./target/debug/hello_world using:
cargo run
- If the code is successfully executed, you will get the following response in your terminal:
Hello World!
- There you have it, your first code ran using cargo.
Comments
Post a Comment