📜 ⬆️ ⬇️

Rust 1.5: Cargo with blackjack

Under the New Year holidays Rust 1.5 was released. Since release 1.6 is nearing, I want to catch up and to talk about what appeared in the latest stable version. Significant changes have affected Cargo!

The main change you get is the cargo install command. Its beauty is the ability to install external plugins on your cargo. A list of available extensions can be found here , crate with plugins most often have the prefix cagro- . I will give an example of using several of them.

cargo-check

The check command allows you to check the code, without assembly. Install:

cargo install cargo-check 

It is very simple to use, just like you use the build command:
')
 cargo check 

The command displays all warnings and errors on the Rust source code of your project.

cargo-edit

The edit command allows you to manage dependencies. Install:

 cargo install cargo-edit 

In fact, 3 commands will be installed immediately: add , rm and list . We use as follows:

 cargo list env_logger ~0.3 hyper ~0.7 json_macro 0.1.1 log ~0.3 lua git: "https://github.com/jcmoyer/rust-lua53" mould path: "C:\\DEVELOPMENT\\rust-dev\\mould" r2d2 0.6.3 r2d2_sqlite 0.0.4 rusqlite 0.6.0 rust-crypto 0.2.34 rustc-serialize ~0.3.16 static-server path: "C:\\DEVELOPMENT\\rust-dev\\static-server" uuid 0.1.18 

Add a dependency with:

 cargo add <crate> [--dev|--build|--optional] [--vers=<ver>|--git=<uri>|--path=<uri>] [options] 


cargo graph

The graph command generates a dependency graph in GraphViz format. You may have seen similar in some libraries on Rust. Install:

 cargo install cargo-graph 

The graph is generated in stdout, so specify the file name:

 cargo graph > deps.dot 

After generating PNG from DOT file:

 dot deps.dot -Tpng -o deps.png 

Here's what happened:

image

cargo-watch

The watch command allows you to automatically collect code when changes are made to files. This approach is very popular in the world of Node (Grunt, Gulp). Install:

 cargo install cargo-watch 

It is also easy to use:

 cargo watch 

Essentially, the command runs an infinite loop with the build and test commands, which simply watches for changes in the files.

By the way, for all commands you can get help through the help command, for example:

 cargo help watch Usage: cargo-watch [watch] [options] cargo watch [options] cargo-watch [watch] [<args>...] cargo watch [<args>...] Options: -h, --help Display this message `cargo watch` can take one or more arguments to pass to cargo. For example, `cargo watch "test ex_ --release"` will run `cargo test ex_ --release` If no arguments are provided, then cargo will run `build` and `test` 


cargo-count

The count command allows you to calculate useful statistics about your code. Install:

 cargo install cargo-count 

Just type:

 cargo count 

And get the following report (he delights me!):

 Gathering information... Language Files Lines Blanks Comments Code -------- ----- ----- ------ -------- ---- TOML 1 19 1 0 18 Python 1 63 11 5 47 Rust 7 824 89 17 718 HTML 41218 5218543 1016734 239837 3961972 CSS 5 907 130 38 739 C 37 19303 2665 1964 14673 C Header 25 3970 1049 1012 1907 C++ Header 1 9 1 3 5 JavaScript 788 4024 591 155 3278 -------- ----- ----- ------ -------- ---- Totals: 42083 5247662 1021271 243031 3983357 


rustfmt

The fmt command allows you to automatically format the code. A close analogue is in Go. Install:

 cargo install rustfmt 

The team has been compiling for quite a long time and is not very stable yet, but already very useful.

Type:

 cargo fmt 

It helps to format the code and shows all not done TODO.

Total

Now you can greatly enhance the convenience of an already beautiful cargo. The prospect pleases.

PS By the way, all commands are full-fledged and stand-alone programs on Rust, which are compiled and added to ~ / .cargo / bin / :

 ls -lah ~/.cargo/bin/  33M drwxr-xr-x 1 denis_000 denis_000 0  15 12:17 . drwxr-xr-x 1 denis_000 denis_000 0  15 16:37 .. -rwxr-xr-x 1 denis_000 denis_000 3,5M  15 12:13 cargo-add.exe -rwxr-xr-x 1 denis_000 denis_000 2,2M  15 12:04 cargo-check.exe -rwxr-xr-x 1 denis_000 denis_000 3,3M  15 16:44 cargo-count.exe -rwxr-xr-x 1 denis_000 denis_000 2,4M  15 12:03 cargo-fmt.exe -rwxr-xr-x 1 denis_000 denis_000 3,1M  15 12:17 cargo-graph.exe -rwxr-xr-x 1 denis_000 denis_000 3,2M  15 12:13 cargo-list.exe -rwxr-xr-x 1 denis_000 denis_000 3,2M  15 12:12 cargo-rm.exe -rwxr-xr-x 1 denis_000 denis_000 3,3M  15 16:37 cargo-watch.exe -rwxr-xr-x 1 denis_000 denis_000 8,4M  15 12:03 rustfmt.exe 

Source: https://habr.com/ru/post/275157/


All Articles