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
}

响应:文件内容。

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