初学RUST-让程序跑起来

周末浏览新闻,有人说RUST是现代版的C语言,我是C语言的忠实拥护者,但是这么多年来,工作并不需要,所以也很少写C代码,不如学习新式RUST语言。今天先把RUST按照好跑起来。就在我的UBUNTUs上完成。

Install Rust - Rust Programming Language

第一步按照工具链

install curl--需要这个工具
https://blog.csdn.net/lyh458/article/details/120263185
sudo snap install curl
--cannot work 
sudo snap remove curl
sudo apt install curl
 

install rust安装rust
sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
...
 stable-x86_64-unknown-linux-gnu installed - rustc 1.59.0 (9d1b2106e 2022-02-23)

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env
如果是公司内网,参考:

if proxy:
https://zhuanlan.zhihu.com/p/308452799
# 用于更新 toolchain
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
# 用于更新 rustup
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
export https_proxy=proxy.xx.xx.xx:80
export http_proxy=proxy.xx.xx.xx:80
然后检查版本

$ rustc --version
rustc 1.59.0 (9d1b2106e 2022-02-23)
$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home:  /home/???/.rustup
stable-x86_64-unknown-linux-gnu (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)
建立一个新项目

$ cargo new scrape_url
     Created binary (application) `scrape_url` package

$ cargo run
   Compiling scrape_url v0.1.0 (/home/zxl/rust/scrape_url)
    Finished dev [unoptimized + debuginfo] target(s) in 1.06s
     Running `target/debug/scrape_url`
Hello, world!
第一个hello world!程序就运行成功了!

当然还要学习一下包依赖:

代码参考:GitHub - tyrchen/geektime-rust: 我的极客时间 Rust 课程的代码仓库,随课程更新

rust用toml文件管理,所以在vscode中加入插件:Better toml: toml文件语法高亮

Cargo.toml文件中增加新的依赖

[dependencies]
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "blocking"] }
html2md = "0.2"
然后改写main.rs: 这个程序的意思是下载rust网站的首页并转换成markdown格式存放到本地文件

use std::fs;
fn main() {
let url = "https://www.rust-lang.org/";
let output = "rust.md";
println!("Fetching url: {}", url);
let body = reqwest::blocking::get(url).unwrap().text().unwrap();
println!("Converting html to markdown...");
let md = html2md::parse_html(&body);
fs::write(output, md.as_bytes()).unwrap();
println!("Converted markdown has been saved in {}.", output);
}

https://github.com/tyrchen/geektime-rust/blob/master/03_grammar/scrape_url/src/main.rs

$ cargo run
    Blocking waiting for file lock on package cache
阻塞了,应该是网络出了问题

rm .package-cache --可以了
cargo run
    Updating crates.io index
       Fetch [                         ]   1.46%, 474.82KiB/s    

error: failed to get `html2md` as a dependency of package `scrape_url v0.1.0 (/home/???/rust/scrape_url)`
SSL error: unknown error; class=Ssl (16)
下载不顺利,改为国内服务器

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

......

   Compiling phf_codegen v0.8.0
   Compiling string_cache_codegen v0.5.1
   Compiling h2 v0.3.11
   Compiling markup5ever v0.10.1
   Compiling html5ever v0.25.1
   Compiling xml5ever v0.16.2
   Compiling hyper v0.14.17
   Compiling markup5ever_rcdom v0.1.0
   Compiling html2md v0.2.13
   Compiling hyper-rustls v0.23.0
   Compiling reqwest v0.11.9
   Compiling scrape_url v0.1.0 (/home/zxl/rust/scrape_url)
    Finished dev [unoptimized + debuginfo] target(s) in 2m 44s
     Running `target/debug/scrape_url`
Fetching url: https://www.rust-lang.org/
Converting html to markdown...
Converted markdown has been saved in rust.md.

 

运行成功!后续有时间研究一下RUST语法。2022年掌握一门新语言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值