Rust日常筆記_持續更新_v1.0.1_Rust

文件名稱 版本號 作者 qq 版本
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-17orrustup 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
}

響應:文件內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章