xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Rust Cargo All In One

Rust Cargo All In One

https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Rust 由工具 rustup 安装和管理。

Rust 有着以 6 星期为周期的 快速版本迭代机制,支持 大量平台,因而不同时期存在大量不同的 Rust 构建版本
rustup 用于管理不同平台下的 Rust 构建版本并使其互相兼容, 支持安装由 Beta 和 Nightly 频道发布的版本,并支持其他用于交叉编译的编译版本。

如果您曾经安装过 rustup,可以执行 rustup update 来升级 Rust。

配置 PATH 环境变量

在 Rust 开发环境中,所有工具都安装在 ~/.cargo/bin 目录中,您可以在这里找到包括 rustccargorustup 在内的 Rust 工具链

Rust 开发者通常会将该目录加入 PATH环境变量中。
在安装过程中,rustup 会尝试配置 PATH。
由于不同平台、命令行 Shell 之间存在差异,rustup 中也可能存在 Bug,因此在终端重启或用户重新登录之前,rustup 对 PATH 的修改可能不会生效,甚至完全无效。

如果安装后在终端尝试执行 rustc --version 失败,那么,以上内容就是最可能的原因。

https://www.rust-lang.org/zh-CN/tools/install

使用 rustup 安装 rust 后,自带了 cargo

$ cargo -h

Rust's package manager

USAGE:
    cargo [+toolchain] [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -V, --version               Print version info and exit
        --list                  List installed commands
        --explain <CODE>        Run `rustc --explain CODE`
    -v, --verbose               Use verbose output (-vv very verbose/build.rs output)
    -q, --quiet                 Do not print cargo log messages
        --color <WHEN>          Coloring: auto, always, never
        --frozen                Require Cargo.lock and cache are up to date
        --locked                Require Cargo.lock is up to date
        --offline               Run without accessing the network
        --config <KEY=VALUE>    Override a configuration value
    -Z <FLAG>                   Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
                                details
    -h, --help                  Print help information

Some common cargo commands are (see all commands with --list):
    build, b    Compile the current package
    check, c    Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc, d      Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    add         Add dependencies to a manifest file
    run, r      Run a binary or example of the local package
    test, t     Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this package to the registry
    install     Install a Rust binary. Default location is $HOME/.cargo/bin
    uninstall   Uninstall a Rust binary

See 'cargo help <command>' for more information on a specific command.

Cargo

https://crates.io/

https://doc.rust-lang.org/cargo/index.html

https://doc.rust-lang.org/cargo/getting-started/installation.html
https://doc.rust-lang.org/cargo/getting-started/first-steps.html

# generate a new package
$ cargo new hello_world

manifest (Cargo.toml)

Rust Cargo.toml

[package]
name = "first_rust_package"
version = "0.0.1"
edition = "2021"
# edition = "2022"

# custom config ✅
author = "xgqfrms"
editor = "vscode"
# warning: unused manifest key: package.author
# warning: unused manifest key: package.editor

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# rust cowsay
# 配置后,无需再次手动导入外部包 external crate
# all dependencies will be auto imported 🚀
ferris-says = "0.2.1"

https://github.com/xgqfrms/rust-in-action/blob/master/test/first_rust_package/Cargo.toml

https://doc.rust-lang.org/cargo/appendix/glossary.html#manifest

package

https://doc.rust-lang.org/cargo/appendix/glossary.html#package

crate

https://doc.rust-lang.org/cargo/appendix/glossary.html#crate

rustc

https://doc.rust-lang.org/rustc/index.html

rustdoc

https://doc.rust-lang.org/rustdoc/index.html

rustwasm

https://rustwasm.github.io/docs/book/

rust-cli

https://rust-cli.github.io/book/index.html

cargo 命令补充

cargo clippy: 类似 eslint,lint工具检查代码可以优化的地方
cargo fmt: 类似 go fmt,代码格式化
cargo tree: 查看第三方库的版本和依赖关系
cargo bench: 运行benchmark(基准测试,性能测试)
cargo udeps (第三方): 检查项目中未使用的依赖
另外 cargo build/run --release 使用 release 编译会比默认的 debug 编译性能提升 10 倍以上,但是 release 缺点是编译速度较慢,而且不会显示 panic backtrace 的具体行号

https://www.runoob.com/rust/cargo-tutorial.html

refs

https://www.runoob.com/rust/rust-tutorial.html

https://www.rust-lang.org/zh-CN/learn



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-09-05 19:21  xgqfrms  阅读(71)  评论(5编辑  收藏  举报