Rust's Build & Package Manager - Cargo
https://www.google.com/imgres?imgurl=https%3A%2F%2Fimages.squarespace-cdn.com%2Fcontent%2Fv1%2F5420d068e4b09194f76b2af6%2F1537411806855-AG8QM728Q0YTRLK3XU7E%2Fcargo_ship_8.jpg%3Fformat%3D1000w&imgrefurl=https%3A%2F%2Fwww.rustafied.com%2Fupdates%2F201

Rust's Build & Package Manager - Cargo

#RustADay

Cargo is Rust build and package manager

If Rust is all installed and up without any issues, you can check for the version on the cargo $ cargo --version

No alt text provided for this image

Creating a Project With Cargo

Let's create a rust project

$ cargo new hello_cargo

$ cd hello_cargo

Now let's see what's inside this project

No alt text provided for this image

It also initializes a new GIT repo along with a .gitignore file.

No alt text provided for this image

The cargo.toml file holds the required configuration based on the purpose and scope of the project

All the source files go into the src folder. Cargo when initializing the project has created a main.rs file under the src folder. This is where we will write our first introductory program

No alt text provided for this image



No alt text provided for this image

The main is the special function that runs first in every executable Rust program

Now that the minimum requirements are in place, lets initiate the build of the project $ cargo build

No alt text provided for this image

This build creates an executable file under a "target/debug" folder of the current working directory.

Then you can simply use this command to run. If all goes well, the output should be right there on the console

$ ./target/debug/hello_cargo

No alt text provided for this image

You can also use $ cargo run to both build and run the project and executables respectively

The command $ cargo check is used to check on for compilations and does not produce any executables. This is lightweight and generally used to check for any compilation errors as and when we need it.

End of the day, the code has to be production ready and $ cargo build --release will compile the code with optimizations and put the executables under the target/release,

Recap

  1. cargo new is used to create a project
  2. cargo build is used to build a project
  3. cargo run does the work of cargo build and cargo run
  4. cargo check can be used to build a project without any errors and binaries
  5. all the binaries are stored under the target/debug directory

要查看或添加评论,请登录

Raj Arun的更多文章

社区洞察

其他会员也浏览了