A Deeper Look at Cargo

Information about A Deeper Look at Cargo

Published on December 13, 2018

Author: AntonWeiss

Source: slideshare.net

Content

1. @[email protected] A Deeper Look at Cargo Ant Weiss, Otomato http://otomato.link

2. @[email protected] Cargo is the Package Manager for Rust ● Basic Scaffolding ● Dependency Mgmt ● Compilation ● Packaging ● Publishing ● Toolchain Expansion

3. @[email protected] The First Step cargo new --bin my-rust-app cargo new --lib my-rust-lib cargo new my-rust-app --vcs git

4. @[email protected] The toml and the lock Cargo.toml: (AKA The Manifest) [package] name = "some-app" version = "0.1.0" authors = ["Anton Weiss <[email protected]>"] [dependencies] tiny_http = "0.6.0" redis = "0.9.0"

5. @[email protected] Cargo.lock Simple rule: Binary: check Cargo.lock into source control Library: put Cargo.lock in .gitignore Don’t use “*” dependencies. Crates.io will reject your crate!

6. @[email protected] Depends On... ● Crates from crates.io [dependencies] chashmap = "2.2.0" ● Git repos [dependencies] evmap = ( git = https://github.com/jonhoo/rust-evmap.git } ● Files in subdirectories of your project [dependencies] hello_utils = { path = "hello_utils" }

7. @[email protected] Versions of Truth Cargo.toml: [dependencies] clap = "2.20.0" >cargo build Updating crates.io index ... Compiling clap v2.32.0 To ensure specific version - use: mylib = "=0.2.3" Maximal Version Resolution = At the time the lockfile is generated - the latest available version is used.

8. @[email protected] Versions of Truth : Caret An update is allowed if the new version number does not modify the left-most non-zero digit in the major, minor, patch grouping: [dependencies] time = "^0.20.0" Examples: ^1.2.3 := >=1.2.3 <2.0.0 ^1.2 := >=1.2.0 <2.0.0 ^1 := >=1.0.0 <2.0.0 ^0.2.3 := >=0.2.3 <0.3.0 ^0.2 := >= 0.2.0 < 0.3.0 ^0.0.3 := >=0.0.3 <0.0.4 ^0.0 := >=0.0.0 <0.1.0 ^0 := >=0.0.0 <1.0.0

9. @[email protected] Versions of Truth : Tilde Specify a minimal version with some ability to update: [dependencies] clap = "~2.3.0" Examples: ~1.2.3 := >=1.2.3 <1.3.0 ~1.2 := >=1.2.0 <1.3.0 ~1 := >=1.0.0 <2.0.0

10. @[email protected] Versions of Truth : Wildcard and Inequality Wildcard - allow for any version where the wildcard is. Examples: * := >=0.0.0 1.* := >=1.0.0 <2.0.0 1.2.* := >=1.2.0 <1.3.0 In/equality - allow manually specifying a version range or an exact version. Examples: >= 1.2.0 > 1 < 2 = 1.2.3

11. @[email protected] Updating dependencies: > cargo update Or > cargo update -p mycrate To update a specific crate.

12. @[email protected] Versions of Truth : Git [dependencies] rand = { git = "https://github.com/rust-lang-nursery/rand" } = Use the latest commit on ’master’ in the linked repo Or - specify branch, tag or commit to use: mylib = { git = "https://mylibrepo" branch = "testing" } mylib = { git = "https://mylibrepo" tag = "v0.12" } mylib = { git = "https://mylibrepo" rev = "ede4591" }

13. @[email protected] Versions of Truth : Path dependencies > cargo new mybin > cd mybin # inside of mybin: > cargo new --lib mylib In Cargo.toml: [dependencies] mylib = { path = "mylib" } Crates with path dependencies are not permitted on crates.io. We’d need to publish a version of mylib to crates.io and specify its version like this: mylib = { path = "mylib" , version = "0.1.0" }

14. @[email protected] Versions of Truth : Overriding dependencies [dependencies] serde_json = '1.0' [patch.crates-io] serde_json = { path = 'serde_json' } [dependencies.mylibrs] git = 'https://github.com/otomato_gh/mylibrs' [patch.'https://github.com/otomato_gh/mylibrs'] mylibrs = { git = 'https://github.com/antweiss/mylibrs', branch = 'patched' }

15. @[email protected] Multi-crate projects a [workspace] is a set of crates that all share the same Cargo.lock and output directory [package] name = "wsbin" version = "0.1.0" authors = ["Anton Weiss <[email protected]>"] [workspace] members = ["wslib", "wsbin2"] cargo build --all

16. @[email protected] Configiring Your Cargo: ● ./.cargo/config ● ../parent/.cargo/config ● $HOME/.cargo/config Some values: [cargo-new] name = "Ant Weiss" email = "[email protected]" # By default `cargo new` will initialize a new Git repository. vcs = "none" [build] jobs = 1 # number of parallel jobs, defaults to # of CPUs rustc = "rustc" # the rust compiler tool

17. @[email protected] Environment Variables ● CARGO - Path to the cargo binary performing the build. ● CARGO_MANIFEST_DIR - The directory containing the manifest of your package. ● CARGO_PKG_VERSION - The full version of your package. ● CARGO_PKG_VERSION_MAJOR - The major version of your package. ● CARGO_PKG_VERSION_MINOR - The minor version of your package. ● CARGO_PKG_VERSION_PATCH - The patch version of your package. ● CARGO_PKG_VERSION_PRE - The pre-release version of your package. ● CARGO_PKG_AUTHORS - Colon separated list of authors from the manifest. ● CARGO_PKG_NAME - The name of your package. ● CARGO_PKG_DESCRIPTION - The description of your package. ● CARGO_PKG_HOMEPAGE - The home page of your package. ● OUT_DIR - If the package has a build script - the folder where it should place its output. let app = env!("CARGO_PKG_NAME");

18. @[email protected] Build scripts ● Use Cases: ○ Building a bundled C library. ○ Finding a C library on the host system. ○ Generating a Rust module from a specification. ○ Performing any platform-specific configuration needed for the crate. [package] # ... build = "build.rs" #the default [build-dependencies] protobuf-codegen-pure = "2.2.0"

19. @[email protected] Example: code generation with protobufs extern crate protobuf_codegen_pure; fn main(){ protobuf_codegen_pure::run(protobuf_codegen_pure::Args { out_dir: "src/protos", input: &["protos/myprotos.proto"], includes: &["protos"], customize: protobuf_codegen_pure::Customize { ..Default::default() }, }).expect("protoc"); }

20. @[email protected] Publishing to Crates.io Register to Crates.io : log in with your Github account. Go to ‘Account Settings’ to get your API token. Log in : $ cargo login <your_API_token> Package the crate: $ cargo package Publish the crate!: $ cargo publish [package] # ... exclude = [ "public/assets/*", "videos/*", ]

21. @[email protected] Integrating with the World CI/CD Tools, IDEs, etc… $ cargo metadata Outputs project structure and dependencies information in JSON $ cargo build --message-format json Outputs: ● compiler errors and warnings, ● produced artifacts, ● results of the build scripts (for example, native dependencies)

22. @[email protected] Custom subcommands $ cargo my_command == ${PATH}/cargo-my_command may use the $CARGO environment variable to call back to Cargo The list of known cargo subcommands.

23. @[email protected] cargo-generate - cargo, make me a project

24. @[email protected] cargo-make - Rust task runner and build tool. [tasks.build] command = "cargo" args = ["build"] dependencies = ["clean"] [tasks.test] command = "cargo" args = ["test"] dependencies = ["clean"] [tasks.my-flow] dependencies = [ "format", "build", "test" ] cargo make --makefile build.toml my-flow build.toml: https://github.com/sagiegurari/cargo-make

25. @[email protected] Unstable Features cargo -Z help Available unstable (nightly-only) flags: -Z avoid-dev-deps -- Avoid installing dev-dependencies if possible -Z minimal-versions -- Install minimal dependency versions instead of maximum -Z no-index-update -- Do not update the registry, avoids a network request for benchmarking -Z offline -- Offline mode that does not perform network requests -Z unstable-options -- Allow the usage of unstable options such as --registry -Z config-profile -- Read profiles from .cargo/config files Run with 'cargo -Z [FLAG] [SUBCOMMAND]'

26. @[email protected] Thank you!!! @antweiss http://otomato.link http://devopstrain.pro

#the presentations

Related presentations


Other presentations created by AntonWeiss

Ninja, Choose Your Weapon!
29. 03. 2015
0 views

Ninja, Choose Your Weapon!

Grooving with Jenkins
18. 07. 2015
0 views

Grooving with Jenkins

Docker, Jenkins, Docker
16. 07. 2017
0 views

Docker, Jenkins, Docker