Rust大杂烩

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

内容简介:不建议使用第三方包管理器安装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大杂烩》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

Learn Python the Hard Way

Learn Python the Hard Way

Zed A. Shaw / Addison-Wesley Professional / 2013-10-11 / USD 39.99

Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called "The Hard Way," but it's re......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码