Rust 语言安装及包管理

Rust 语言

一门专注安全的编程语言
让我们开始吧
在这里插入图片描述

对于安装现在的Rust是非常友好的,官网会根据系统推荐安装方式,如果是Windows系统,可以直接安装在Windows上,也可以安装到wsl中。

两种安装方式这边推荐安装虚拟机中,因为如果安装在Windows中需要提前安装Visual Studio

一、安装

我用的是百度云服务器,系统为Ubunru 18.04.

吐槽一下百度的云服务器,竟然没有快照

$ curl https://sh.rustup.rs -sSf | sh
……
Rust is installed now. Great!    # 安装成功
$ source $HOME/.cargo/env 		 # 设置环境变量

二、国内服务包代理

$ cd ~/.cargo
$ vim config
[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

三、第一个Rust程序

R语言的文件扩展名为.rs,可通过cargo创建项目

$ cargo new hello # 创建一个名为hello的项目
$ cargo build # 编译项目
$ cargo run # 运行项目

创建项目后会自动生产一个main.rs源代码

use std::io;

fn main() {
    println!("Hello, Please input guess!");
    let mut guess = String::new();
    io::stdin().read_line(&mut guess)
        .expect("Failed to read line");
    println!("You guess:{}",guess);
}
------------上面是代码,下面是输出结果--------------
Hello, Please input guess!
5
You guess:5

四、cargo包管理工具

$cargo --help
Rust's package manager

Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
       cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]...

Options:
  -V, --version             Print version info and exit
      --list                List installed commands
      --explain <CODE>      Provide a detailed explanation of a rustc error message
  -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
  -C <DIRECTORY>            Change to DIRECTORY before doing anything (nightly-only)
      --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

Commands:
    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
    remove      Remove dependencies from 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 all commands with --list

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

cargo官方文档
cargo备忘清单

这里关注几个常用的命令

  • cargo -V -查看cargo版本
  • cargo new xxx - 创建一个新的项目
  • cargo install xxx - 安装已有轮子
  • cargo run xxx -编译并运行项目
  • cargo build xxx -仅编译项目

五、cargo.toml

Cargo.toml是Rust编程语言的清单文件,它用于保存项目的元数据和依赖项。

[package]                              - 定义项目元信息
name = "format"	                       - 项目名称
version = "0.1.0"                      - 项目版本号
edition = "2021"                       - 用于指定项目所使用的 Rust Edition 不指定默认2015,使用caogo new创建项目时自动创建

[dependencies]                         - 项目依赖包

[source.ustc]                          - 制定包的安装源
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

更多关于cargo.toml解析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值