Rust大杂烩

栏目: 编程语言 · Rust · 发布时间: 5年前

内容简介:不建议使用第三方包管理器安装Rust。建议使用官方的脚本一键安装安装脚本:

不建议使用第三方包管理器安装Rust。

建议使用官方的脚本一键安装 Rustup (Rust版本管理器)到$HOME目录。不需要Root权限,也便于以后切换版本

安装脚本: curl https://sh.rustup.rs -sSf | sh

安装完成后,需要将 Cargobin 目录添加到 PATH 环境变量

这个 bin 目录大概有这些文件:

cargo        cargo-fmt    rust-gdb     rustc        rustfmt
cargo-clippy rls          rust-lldb    rustdoc      rustup

永久添加: 编辑 ~/.bashrc~/.zshrc ,添加一行

export PATH=$HOME/.cargo/bin:$PATH

一次性添加: source ~/.cargo/env

Rust之HelloWorld

cargo new hello-rust
cd hello-rust
cargo run

目录结构

├── Cargo.toml
└── src
    └── main.rs

Cargo.toml

[package]
name = "hello-rust"
version = "0.1.0"
authors = ["BaiJiFeiLong <baijifeilong@gmail.com>"]
edition = "2018"

[dependencies]

src/main.rs

fn main() {
    println!("Hello, world!");
}

控制台输出

Hello, world!

Rust引入第三方库

1. 编辑Cargo.toml,添加依赖项

[package]
name = "untitled"
version = "0.1.0"
authors = ["BaiJiFeiLong <baijifeilong@gmail.com>"]
edition = "2018"

[dependencies]
ferris-says = "0.1"

2. 安装依赖

cargo build

或者

cargo run

3. 使用依赖

src/main.rs

fn main() {
    use ferris_says::*;
    use std::io::{stdout, BufWriter};

    let stdout = stdout();
    let out = b"Hello World";
    let width = 24;

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

控制台输出

----------------------------
| Hello World              |
----------------------------
              \
               \
                  _~^~^~_
              \) /  o o  \ (/
                '_   -   _'
                / '-----' \

文章首发: https://baijifeilong.github.io


以上所述就是小编给大家介绍的《Rust大杂烩》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Algorithms: An Intuitive Approach

Distributed Algorithms: An Intuitive Approach

Wan Fokkink / MIT Press / 2018-2-2 / USD 48.00

The new edition of a guide to distributed algorithms that emphasizes examples and exercises rather than the intricacies of mathematical models. This book offers students and researchers a guide to ......一起来看看 《Distributed Algorithms: An Intuitive Approach》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换