Rust 入门

这是Rust入门第一课,我们要学会安装rust,编译一个rust文件,还有如何升级rust工具链

工具安装

下载

  1. 适合任何平台
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. mac os 平台
    推荐使用brew install rust来安装

验证下载是否成功

rustc --version返回版本信息,则说明rust安装成功

工具介绍

rustc 简介

rust编译器,负责处理,编译,链接rust语言源文件,比如说,要编译file.rs文件,可以使用命令rustc file.rs

rustup 简介

rust工具链安装器,负责安装,管理和更新rust工具链,比如说,要更新所有工具链, 也就是说升级rust,可以使用命令 rustup update

cargo 简介

rust包管理器,负责管理rust工程和他们的module依赖,这是是我们学习最常用的一个工具,我们来看看这个命令包含哪些参数

➜  ~ cargo
Rust's package manager

USAGE:
    cargo [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             No output printed to stdout
        --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
    -Z <FLAG>...            Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help              Prints help information

Some common cargo commands are (see all commands with --list):
    build       Compile the current package
    check       Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc         Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    run         Run a binary or example of the local package
    test        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

实战

创建一个工程

下面的命令是创建一个hello的工程,–bin指明创建一个二进制文件,是binary的缩写
cargo new hello --bin

编写源码

在目录hello下创建文件 hello.rs

fn main() {
  println!("hello world");
}

运行

下面的命令是运行一个rust工程
cargo run

总结

我们学会了如何利用cargo工具创建一个工程,编译源码,其实原理就是cargo编译源码时调用了rustc工具

原文:https://dev4mobiles.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值