Rust 学习笔记-1-入门篇

安装Rust

1,对于Unix/Linux而言,安装命令如下:

curl https://sh.rustup.rs -sSf | sh

注:

1,Windows 平台的安装,可参考:https://forge.rust-lang.org/infra/other-installation-methods.html 和 https://www.rust-lang.org/zh-CN/tools/install

 2,运行命令后,出现提示信息如下。主要是告知安装位置以及如何修改安装位置。若无需更改,直接回车即可。

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/ubuntu/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /home/ubuntu/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/ubuntu/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile file located at:

  /home/ubuntu/.profile

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>

3, 回车后执行默认安装,等待其安装完成。

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2020-07-30, rust version 1.45.1 (c367798cf 2020-07-26)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 12.2 MiB /  12.2 MiB (100 %)  11.2 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 15.8 MiB /  15.8 MiB (100 %)   6.0 MiB/s in  2s ETA:  0s
info: downloading component 'rustc'
 47.3 MiB /  47.3 MiB (100 %)   5.8 MiB/s in  8s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: Defaulting to 500.0 MiB unpack ram
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.2 MiB /  12.2 MiB (100 %)   6.0 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
 15.8 MiB /  15.8 MiB (100 %)  11.0 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 47.3 MiB /  47.3 MiB (100 %)  11.6 MiB/s in  4s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.45.1 (c367798cf 2020-07-26)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run source $HOME/.cargo/env

4,在当前shell 导入PATH环境变量。

source $HOME/.cargo/env

5,运行rustc --version查看版本(当前的stable版是1.45.1),验证一下安装。

rustc --version
rustc 1.45.1 (c367798cf 2020-07-26)

 开发环境配置

1,个人选择的编辑器是vscode。在vscode 扩展中搜索"Rust"和"rust-analyzer",安装便可。

注:

1,Rust官方支持8款主流的编辑器。参见:https://www.rust-lang.org/zh-CN/tools

2,于个人而言,vscode安装在本地(windows),采用remote development的方式连接到远程主机(ubuntu)。

"Hello World"

 1,按惯例,撸一把rust版的hello world。先建立hello wolrd的目录。

 mkdir -p  ~/programming/rust/hello_world

 2,打开vscode,连接到远程主机,打开刚才新建的目录,然后新建一个名为hello.rs文件。键入如下code,并保存。

注:

1,此code 可直接从官网示例中复制:https://doc.rust-lang.org/stable/rust-by-example/hello.html

3, 运行Rust编译器,得到二进制文件:

rustc hello.rs

4,执行刚才生成的二进制,屏幕输出"Hello World!"

./hello
Hello World!

"Hello Rust"

接下来是另一个入门示例。

1,首先用 Cargo 创建一个新项目,执行以下命令。

cargo new hello-rust

2, 生成一个hello-rust目录,其中包含以下文件:

Cargo.toml 为 Rust 的清单文件。其中包含了项目的元数据和依赖库。

src/main.rs 为编写应用代码的地方。

3,进入到hello-rust目录,运行:

cargo run

可以看到如下输出。

   Compiling hello-rust v0.1.0 (/home/ubuntu/programming/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/hello-rust`
Hello, world!

 4,在项目中添加依赖库。这个示例添加的库是:ferris-says。打开https://crates.io/,搜索ferris-says。然后在Cargo.toml文件中,添加:

[dependencies]
ferris-says = "0.2"

 然后运行:

cargo build

Cargo 就会安装该依赖。它会创建一个新文件 Cargo.lock,该文件记录了本地所用依赖库的精确版本。过程的输出信息如下:

cargo build
    Blocking waiting for file lock on build directory
   Compiling adler v0.2.3
   Compiling gimli v0.22.0
   Compiling unicode-width v0.1.8
   Compiling object v0.20.0
   Compiling cfg-if v0.1.10
   Compiling rustc-demangle v0.1.16
   Compiling vec_map v0.8.2
   Compiling ansi_term v0.11.0
   Compiling strsim v0.8.0
   Compiling smallvec v0.4.5
   Compiling miniz_oxide v0.4.0
   Compiling textwrap v0.11.0
   Compiling addr2line v0.13.0
   Compiling bitflags v1.2.1
   Compiling libc v0.2.74
   Compiling backtrace v0.3.50
   Compiling atty v0.2.14
   Compiling clap v2.33.1
   Compiling error-chain v0.10.0
   Compiling ferris-says v0.2.0
   Compiling hello-rust v0.1.0 (/home/ubuntu/programming/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 21.81s

 5,接下来在code中使用ferris-says库的say函数。将main.rs中的代码修改成如下,并保存。

extern crate ferris_says;

use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(message.as_bytes(), width, &mut writer).unwrap();
}

然后运行:

cargo run

可以得到如下输出信息:

cargo run
   Compiling hello-rust v0.1.0 (/home/ubuntu/programming/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.81s
     Running `target/debug/hello-rust`
 __________________________
< Hello fellow Rustaceans! >
 --------------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \

知识点汇总

  • crates.io,是 Rust 包的仓库。在 Rust 中,通常把包称作“crates”。
  • cargo 是 Rust 的构建工具和包管理器。

cargo build 可以构建项目
cargo run 可以运行项目
cargo test 可以测试项目
cargo doc 可以为项目构建文档
cargo publish 可以将库发布到 crates.io
  • Rust 由工具 rustup 安装和管理。安装 Rust 的主要方式是通过 Rustup 这一工具,它既是一个 Rust 安装器又是一个版本管理工具。Rust 有着以 6 星期为周期的 快速版本迭代机制,支持 大量平台,因而不同时期存在大量不同的 Rust 构建版本。 rustup 用于管理不同平台下的 Rust 构建版本并使其互相兼容, 支持安装由 Beta 和 Nightly 频道发布的版本,并支持其他用于交叉编译的编译版本。

// 更新rustup本身 
$ rustup self update 
// 卸载rust所有程序 
$ rustup self uninstall 
// 更新工具链 
$ rustup update
  • Rust有3个发布频道:stable/beta/nightly。使用nightly的示例:

// 安装nightly版本的编译工具链 
$ rustup install nightly 
// 设置默认工具链是nightly版本
$ rustup default nightly
  • 在 Rust 开发环境中,所有工具都安装在 ~/.cargo/bin 目录中,可以在这里找到包括 rustccargo 和 rustup 在内的 Rust 工具链。该目录下的全部文件如下:
cargo  cargo-clippy  cargo-fmt  cargo-miri  clippy-driver  rls  rustc  rustdoc  rustfmt  rust-gdb  rust-lldb  rustup

其中,rustc是编译器,cargo是包管理器,cargo-fmt和 rustfmt.exe是源代码格式化工具,rust-gdb和rust-lldb是调试器, rustdoc是文档生成器,rls是为编辑器准备的代码提示 工具,rustup是管理这套工具链下载更新的工具。 

参考链接:

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

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

https://doc.rust-lang.org/stable/rust-by-example/hello.html

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值