项目作者: peterschutt

项目描述 :
An introduction to Cargo
高级语言: Rust
项目地址: git://github.com/peterschutt/rust-hello-cargo.git
创建时间: 2019-12-13T23:24:40Z
项目社区:https://github.com/peterschutt/rust-hello-cargo

开源协议:Apache License 2.0

下载


rust-hello-cargo

An introduction to Cargo. This is the Hello, Cargo! example from The Rust Programming Language book.

What is Cargo?

Rusts build system and package manager.

  • Builds your code
  • Downloads the dependencies of your code
  • Builds those dependencies
  • Installed with Rust through main channels
  • Ensures a repeatable build

Cargo does 4 things:

  • Introduces two metadata files with various bits of package information.
  • Fetches and builds your package’s dependencies.
  • Invokes rustc or another build tool with the correct parameters to build your package.
  • Introduces conventions to make working with Rust packages easier.

cargo new

This command will create a new Cargo package in the given directory. This includes a simple template with a Cargo.toml manifest, sample source file, and a VCS ignore file. If the directory is not already in a VCS repository, then a new repository is created…

  1. > cargo new hello_cargo

cargo new hello_cargo creates a new project called, “hello_cargo” and Cargo creates its files in a directory of the same name. This is the contents of .\hello_cargo:

  1. C:.
  2. .gitignore
  3. Cargo.toml
  4. └───src
  5. main.rs

Cargo.toml

The Cargo.toml file for each package is called its manifest. Every manifest file consists of one or more sections.

  1. [package]
  2. name = "hello_cargo"
  3. version = "0.1.0"
  4. authors = ["Peter <peter@rustacean.com>"]
  5. edition = "2018"
  6. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  7. [dependencies]

[package]

  • Indicates section of statements necessary for program compilation.
  • Name and email are sourced from environment, so edit if necessary.

[dependencies]

  • Section to list project dependencies.
  • In Rust, packages are referred to as Crates.
  • This example has no dependencies.

.\src directory

  • Cargo expects source files to live inside .\src directory.
  • Top level of a project directory is for README, LICENSE and other non-code related files.
  • Everything has its place!

cargo build

Compile local packages and all of their dependencies.

  1. PS C:\Users\peter\Documents\rust-projects\hello_cargo> cargo build
  2. Compiling hello_cargo v0.1.0 (C:\Users\peter\Documents\rust-projects\hello_cargo)
  3. Finished dev [unoptimized + debuginfo] target(s) in 3.67s

Directory structure after build:

  1. C:.
  2. .gitignore
  3. Cargo.lock
  4. Cargo.toml
  5. ├───src
  6. main.rs
  7. └───target
  8. .rustc_info.json
  9. └───debug
  10. .cargo-lock
  11. hello_cargo.d
  12. hello_cargo.exe
  13. hello_cargo.pdb
  14. ├───.fingerprint
  15. └───hello_cargo-a395dae34745bcde
  16. bin-hello_cargo-a395dae34745bcde
  17. bin-hello_cargo-a395dae34745bcde.json
  18. dep-bin-hello_cargo-a395dae34745bcde
  19. invoked.timestamp
  20. ├───build
  21. ├───deps
  22. hello_cargo-a395dae34745bcde.d
  23. hello_cargo-a395dae34745bcde.exe
  24. hello_cargo-a395dae34745bcde.pdb
  25. ├───examples
  26. └───incremental
  27. └───hello_cargo-22f3bxmqxbfgr
  28. s-fiqu004mlt-oglb32.lock
  29. └───s-fiqu004mlt-oglb32-140ltojrvnqu3
  30. 1u61dt6owvp097js.o
  31. 2dx4eoeffqq8ely7.o
  32. 3ws2uvgmoh0tnkqn.o
  33. 4fgpioc2zyfydswo.o
  34. 58xt47pe0vk38jpg.o
  35. dep-graph.bin
  36. kvbkhulsununltn.o
  37. query-cache.bin
  38. work-products.bin
  • Executable file at .\target\debug\hello_cargo.exe.
  • Cargo.lock at top level keeps track of exact version of project dependencies. Never manually edit this file, Cargo manages it.
  • --release flag compiles the project with optimisations that result in a slower compilation but faster executable. Always use --release flag for profiling and production releases. E.g.:
    1. PS C:\Users\peter\Documents\rust-projects\hello_cargo> cargo build --release
    2. Compiling hello_cargo v0.1.0 (C:\Users\peter\Documents\rust-projects\hello_cargo)
    3. Finished release [optimized] target(s) in 0.36s
    Adds the .\target\release\ directory:
    1. ...
    2. └───target
    3. ...
    4. └───release
    5. .cargo-lock
    6. hello_cargo.d
    7. hello_cargo.exe
    8. hello_cargo.pdb
    9. ├───.fingerprint
    10. └───hello_cargo-147a78feb613f770
    11. bin-hello_cargo-147a78feb613f770
    12. bin-hello_cargo-147a78feb613f770.json
    13. dep-bin-hello_cargo-147a78feb613f770
    14. invoked.timestamp
    15. ├───build
    16. ├───deps
    17. hello_cargo-147a78feb613f770.d
    18. hello_cargo-147a78feb613f770.exe
    19. hello_cargo-147a78feb613f770.pdb
    20. ├───examples
    21. └───incremental

cargo run

Run the current package

  • Will compile the package if necessary.

cargo check

Check a local package and all of its dependencies for errors. This will essentially compile the packages without performing the final step of code generation, which is faster than running cargo build.

  • Allows for faster dev iteration with frequent code checking.