文件名稱 | 版本號 | 作者 | 版本 | |
---|---|---|---|---|
Rust日常筆記_持續更新 | v1.0.1 | 學生宮布 | 8416837 | rust 1.44 |
文章目錄
Rust開發環境
安裝基礎環境
Win OS環境
使用Chocolatey
軟件包工具安裝Rust,Chocolatey
的安裝教程見Chocolatey教程。或者使用其它方式安裝,下載Rustup。
如果是巧克力方式安裝:
執行:choco search rust
rust包被發現,但是我們不需要直接安裝rust,先安裝rustup
- 設置rustup國內鏡像:
set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
以上鏡像配置當前頁面有效。有可能設置鏡像後報ssl
錯誤。
如果不起效,使用PowerShell設置:
$ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static'
$ENV:RUSTUP_UPDATE_ROOT='https://mirrors.ustc.edu.cn/rust-static/rustup'
choco install rustup --pre
輸入yes
,開始安裝
Installing rustup-init...
晚上安裝時,在這個初始化卡住了
- 安裝界面,查看方便點
choco install ChocolateyGUI
打開: - 查看rustup和cargo.Cargo:包管理器。類似npm、Java的Maven。
- rustup show 查看工具鏈
沒有
看下存在的工具鏈,也可以增加工具鏈
rustup toolchain list
設置默認工具鏈
rustup default stable-x86_64-pc-windows-msvc
- 組件是必需的:
裝上
rustup component add rust-src
很遺憾,不支持組件:
- 安裝其它版本的
rustup toolchain install nightly-2019-01-17
orrustup toolchain add nightly
如果報ssl錯誤,則先不要用國內鏡像
好的開始:
移除這個
rustup toolchain remove stable
設置新的默認
rustup default nightly
- 防止報錯:找不到
link.exe
。請安裝VCBuild tools 2015 - 檢查編譯器是否存在
rustc -V
:
在線的IDE
Rust In IDEA
- 安裝 Rust
有點慢 - 重啓後,New Project,選擇Rust
按照上文了設置了默認工具鏈,新建項目纔有顯示rust版本1.44.1:
選擇或不選擇工具鏈、標準庫
界面:
- Cargo.toml
依賴管理。如:
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
如果不引入依賴,則會報錯找不到類。
Hello, world!
向main.rs
拷入代碼:
fn main() {
const X: i32 = 9;
println!("{}", "Hello, world!".to_owned() + &X.to_string());
}
執行結果:
Hello, world!9
有什麼感想?比如說,類型是不是比較嚴格?——Java、Js用+號就可以連接兩個對象了
Rust In VsCode
奮鬥
學習指導
網站
執行rustup
命令:rustup doc --book
,打開web頁面,可獲得匹配當前rust版本的文檔,保證例子可以跑起來。
Getting Started
老規矩,Hello Everyone !
- 代碼
fn main() {
// let x = 5;
let x = "9";
println!("{}", "Hello, world!".to_owned() + x);
}
- 疑問1:爲什麼字符串連接要用到函數
to_owned
?
答:語法使然 - 疑問2:爲什麼連接個字符串,打印時,還要加
{}
進行格式化?
小實踐
簡單功能
- 簡單的線程
代碼:
use std::thread;
use std::time::Duration;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi main number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
控制檯輸出:
- 簡單的接口調用:
在Cargo.toml加入對應依賴:
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
代碼:
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use futures::Future;
use hyper::{Client, Uri};
use tokio_core::reactor::Core;
fn main() {
// Core is the Tokio event loop used for making a non-blocking request
let mut core = Core::new().unwrap();
let client = Client::new();
let url: Uri = "http://kunpeng.csdn.net/ad/474".parse().unwrap();
assert_eq!(url.query(), None);
let _request_result = core.run(client
.get(url)
.map(|res| {
println!("Response: {}", res.status());
println!("Response: {:?}", res.body());
})
.map_err(|err| {
println!("Error: {}", err);
})
);
}
響應:
Body流需要轉成明文。
- 若引入了外部依賴,需要下載,設置鏡像庫下載:
找到.cargo
目錄,新建文件名稱config
,填入下述內容:
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
- 讀取簡單的文本文件
在Cargo.toml加入對應依賴:
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
代碼:
fn main() {
// / Create a path to the desired file
let path = Path::new("C:\\dev\\test\\project\\backend\\rust\\rustTestFourth\\src\\One.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that
// describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
// Read the file contents into a string, returns `io::Result<usize>`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display,
why.description()),
Ok(_) => print!("{} contains:\\n{}", display, s),
}
// `file` goes out of scope, and the "hello.txt" file gets closed
}
響應:文件內容。