开发整理,常用技术栈。
依赖
[dependencies]
log = "0.4.14"
simple_logger = "2.1.0"
time = "0.3.7"
初始化
use log::LevelFilter;
use simple_logger::SimpleLogger;
use time::UtcOffset;
pub fn init_log() {
SimpleLogger::new()
.with_level(LevelFilter::Info)
.with_colors(true)
.with_utc_offset(UtcOffset::from_hms(8, 0, 0).unwrap())
.init()
.unwrap();
}
特别注意:从控制台输入,会隐含读入\n换行符等,需要进行trim处理,否则使用match等方法匹配失败。
// 标准输出
println
// 标准错误
eprintln
// 输入
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.expect("输入错误!");
// 输出
println!("你输入的内容是 : {}", input);
let user_choice = menu_handle();
// 通过String.as_str()方法可快速转换,此处为处理换行符,调用trim方法,trim方法隐含有转&str的能力
match user_choice.trim() {
"1" => println!("用户选择1"),
"2" => println!("用户选择2"),
_ => println!("用户选择功能项无效"),
}
print!("输入文件名:");
解析:rust标准输出是行缓冲的,只有到了行尾才显示,想要输出需要手动刷新。
io::stdout().flush();
use std::thread::sleep;
// 线程睡眠
sleep(Duration::from_secs(1));
calamine = "0.18.0"
// calamine = "0.18.0"
fn data_read(file_path: String) {
//Excel读取
let mut excel: Xlsx<_> = open_workbook(file_path).expect("打开Excel文件失败!");
if let Some(Ok(r)) = excel.worksheet_range_at(0) {
let (h, w) = r.get_size();
log::info!("读取到Excel文件共{}行, {}列", h, w);
let mut title_vec: Vec<String> = Vec::new();
for i in 0..w {
let title_name = r.get_value((0, i as u32)).unwrap().to_string();
title_vec.push(title_name);
}
println!("titles:{:?}", title_vec);
// 跳过首行标题行
let rows = r.rows().skip(1);
for row in rows {
println!("row[5]={:?}", row[5]);
}
}
}
let num = input.trim().parse::<i32>().unwrap();
colored = "2.0.0"
"this is blue".blue();
"this is red".red();
"this is red on blue".red().on_blue();
"this is also red on blue".on_blue().red();
"you can use truecolor values too!".truecolor(0, 255, 136);
"background truecolor also works :)".on_truecolor(135, 28, 167);
"bright colors are welcome as well".on_bright_blue().bright_red();
"you can also make bold comments".bold();
println!("{} {} {}", "or use".cyan(), "any".italic().yellow(), "string type".cyan());
"or change advice. This is red".yellow().blue().red();
"or clear things up. This is default color and style".red().bold().clear();
"purple and magenta are the same".purple().magenta();
"and so are normal and clear".normal().clear();
"you can specify color by string".color("blue").on_color("red");
String::from("this also works!").green().bold();
format!("{:30}", "format works as expected. This will be padded".blue());
format!("{:.3}", "and this will be green but truncated to 3 chars".green());
//注意:find查出是索引号,中文存储UTF8编码,一个中文占3个字节。
//总长度
let len = company.len_utf8();
let index = company_name.find("区").unwrap();
let end = min(company_name.len(), index + 3 * 3);
company_name[index + 3..end].to_string()
let handle = tokio::spawn(async move {
sleep(Duration::from_secs(interval_time as u64));
});
handle.await.unwrap();
reqwest = { version = "0.11", features = "blocking"}
// reqwest可以使用阻塞方式
let body = reqwest::blocking::get(url).unwrap().text().unwrap();
html2md = "0.2"
html2md::parse_html(&body);
clap = "3.0.0-beta.4"
# actix-web
# rocket
# warp
# axum
# Web技术栈:tauri
# 原生 GUI:druid、iced 和 sixtyfps。
//Vec HashMap都会扩容
//显式调用shrink_to_fit方法,可缩小容量
# 目前较活跃的库:用纯Rust编写的十进制实现,适合财务计算
rust_decimal = "1.25.0"
// Rust 支持几乎所有主流的数据库,包括但不限于 MySQL、Postgres、Redis、RocksDB、Cassandra、MongoDB、ScyllaDB、CouchDB 等等
// 使用 ORM,可以用: diesel,或者 sea-orm。
// 如果你享受直接但安全的 SQL 查询,可以使用 sqlx。
//Rust 有对标 puppeteer 的 headless_chrome
//以及对标 selenium 的 thirtyfour 和 fantoccini。
// 处理 Kubernetes API 的:kube-rs
// 用 wasm-pack 和 wasm-bindgen,不但生成 wasm,同时还生成 ts/js 调用 wasm 的代码
nipper = "0.1.9"
tendril = "0.4.2"
regex = "1.5.6"
/// 正则处理内容,多空格自动替换
pub async fn content_handle(content: &str) -> String {
let r = Regex::new(r"\s+").unwrap();
let res = r.replace_all(content, " ");
res.to_string()
}
// 引入依赖:async-recursion = "1.0.0"
// 递归方法增加:#[async_recursion(?Send)]
let mut hasher = DefaultHasher::new();
url.hash(&mut hasher);
let id = hasher.finish();
# serde: required if you are going to use documents
serde = { version="1.0", features = ["derive"] }
# serde_json: required in some parts of this guide
serde_json = "1.0"
#[derive(Serialize, Deserialize)]