内容简介:不建议使用第三方包管理器安装Rust。建议使用官方的脚本一键安装安装脚本:
不建议使用第三方包管理器安装Rust。
建议使用官方的脚本一键安装 Rustup
(Rust版本管理器)到$HOME目录。不需要Root权限,也便于以后切换版本
安装脚本: curl https://sh.rustup.rs -sSf | sh
安装完成后,需要将 Cargo
的 bin
目录添加到 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 \ (/
'_ - _'
/ '-----' \
以上所述就是小编给大家介绍的《Rust大杂烩》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Art of Computer Programming, Volume 3
Donald E. Knuth / Addison-Wesley Professional / 1998-05-04 / USD 74.99
Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 3》 这本书的介绍吧!