Skip to content

Commit

Permalink
Rust整理
Browse files Browse the repository at this point in the history
  • Loading branch information
pickjob committed Apr 28, 2022
1 parent cecfb39 commit 57f08b2
Show file tree
Hide file tree
Showing 62 changed files with 1,311 additions and 614 deletions.
89 changes: 44 additions & 45 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,45 @@ name = "rust-starter"
version = "0.1.0"

[dependencies]
# Result包装
# 异常处理
anyhow = "*"
# 异步支持
futures = "*"
# 字节流处理
bytes = "*"
# 日志
log = { version = "*", features = ["max_level_trace", "release_max_level_info"] }
flexi_logger = { version = "*", features = ["async", "specfile", "compress"] }
# 时间
chrono = { version = "*", features = ["serde"] }
# 编码
hex = "*"
base64 = "*"
urlencoding = "*"
# 正则
regex = "*"
# 序列化、反序列化
serde = { version = "*", features = ["derive"] }
serde_json = "*"
serde_yaml = "*"
serde_qs = "*"
serde-transcode = "*"
# 时间
chrono = { version = "*", features = ["serde"] }
# 日志
log = { version = "*", features = ["max_level_trace", "release_max_level_info"] }
flexi_logger = { version = "*", features = ["async", "specfile", "compress"] }
# 随机数
rand = "*"
# 字节流处理
bytes = "*"
# 压缩
flate2 = "*"
# 正则
regex = "*"
# Http客户端
ureq = {version = "2.0.1", features = ["charset", "json"]}
ureq = {version = "*", features = ["charset", "json"]}
# sqlite
rusqlite = "*"
# 并发
rayon = "*"
crossbeam = "*"
# 异步支持
futures = "*"
# async-std
async-std = {version = "*", features = ["attributes"]}
# 异步函数递归调用
async-recursion = "*"

[[example]]
name = "template"
path = "examples/template.rs"

[[example]]
name = "template_async"
path = "examples/template_async.rs"

[[example]]
name = "fmt"
path = "examples/std/fmt.rs"
Expand Down Expand Up @@ -83,56 +78,60 @@ path = "examples/std/concurrent/rwlock.rs"

[[example]]
name = "generic-class"
path = "examples/quirks/trait-object/generic-class.rs"
path = "examples/features/generic-class.rs"

[[example]]
name = "anyhow"
path = "examples/frame/anyhow.rs"
path = "examples/frame/error/anyhow.rs"

[[example]]
name = "base64"
path = "examples/frame/base64.rs"
name = "log"
path = "examples/frame/log/log.rs"

[[example]]
name = "urlencoding"
path = "examples/frame/urlencoding.rs"
name = "chrono"
path = "examples/frame/time/chrono.rs"

[[example]]
name = "hex"
path = "examples/frame/hex.rs"
path = "examples/frame/codec/hex.rs"

[[example]]
name = "log"
path = "examples/frame/log.rs"
name = "base64"
path = "examples/frame/codec/base64.rs"

[[example]]
name = "log2"
path = "examples/frame/log2.rs"
name = "urlencoding"
path = "examples/frame/codec/urlencoding.rs"

[[example]]
name = "rand"
path = "examples/frame/rand.rs"
name = "serde"
path = "examples/frame/serialize/serde.rs"

[[example]]
name = "serde"
path = "examples/frame/serde.rs"
name = "ureq"
path = "examples/frame/http/ureq.rs"

[[example]]
name = "rand"
path = "examples/frame/utils/rand.rs"

[[example]]
name = "threadpool"
path = "examples/frame/threadpool.rs"
name = "flate2"
path = "examples/frame/utils/flate2.rs"

[[example]]
name = "ureq"
path = "examples/frame/ureq.rs"
name = "regex"
path = "examples/frame/utils/regex.rs"

[[example]]
name = "sqlite"
path = "examples/frame/sqlite.rs"
path = "examples/frame/database/sqlite.rs"

[[example]]
name = "async_std"
path = "examples/frame/async_std.rs"
name = "rayon"
path = "examples/frame/concurrent/rayon.rs"

[[example]]
name = "runtime_reflection_system"
path = "examples/quirks/runtime_reflection_system.rs"
name = "async_std"
path = "examples/frame/async/async-std.rs"
58 changes: 10 additions & 48 deletions doc/Rust基础.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 命名:
// Types / Traits / Enum variants: UpperCamelCase
// Modules / Methods / Functions / Macros / Local variables: snake_case
// 使用 mut、move、ref 方法后缀提示参数可变性
// prefer using the borrowed type over borrowing the owned type
// &str over &String
// &[T] over &Vec<T>
Expand All @@ -19,54 +20,15 @@
// type FnPtr = fn() -> String;
// prefer using Fn trait objects
// concatenating strings with format!

// Convert:
// as_(std::convert::AsRef, std::convert::AsMut): Free
// into_(std::convert::From, std::convert::Into): Variable
// to_(std::borrow::ToOwned): Expensive
// usage:
// str.as_bytes(): str => &[u8]
// str.as_bytes_mut(): str => &mut [u8]
// str.to_owned(): str => String
// str::from_utf8(): &[u8] => &str
// str::from_utf8_mut(): &mut [u8] => &mut str
//
// String.as_bytes(): String => &[u8]
// String.as_str(): String => &str
// String.as_mut_str(): String => &mut str
// String::as_ref(): String => [u8]
// String.into_bytes(): String => Vec<u8>
// String.borrow(): String => &str
// String.borrow_mut(&mut self): String =>&mut str
// String::from_utf8_lossy(): &[u8] => Cow<'_, str>
//
// Path.to_owned(): Path => PathBuf
// Deref:
// Vec<T> impl Deref<Target = [T]>
// String impl Deref<Target = str>
// PathBuf impl Deref<Path>
//
// Rc<T> impl<T> Deref<Target = T> where T: ?Sized,
// Arc<T> impl<T> Deref for where T: ?Sized,
// Box<T> impl<T> Deref<Target = T> where T: ?Sized,
// Cow<T> impl<T> Deref<Target = T> where T: ToOwned + ?Sized,
// Default:
// String impl Default
// Option<T> impl<T> Default
//
// Rc<T> impl<T> Default where T: Default,
// Arc<T> impl<T> Default where T: Default,
// Box<[T]> impl<T> Default
// Cell<T> impl<T> Default where T: Default,
// RefCell<T> impl<T> Default where T: Default,
// Cow<T> impl Default where T: ToOwned + ?Sized, <B as ToOwned>::Owned: Default,
//
// Vec<T>, HashMap<K, V>, VecDeque<T> impl Default
// Iterator:
// fn iter(&self) -> Iter // Iter implements Iterator<Item = &U>
// fn iter_mut(&mut self) -> IterMut // IterMut implements Iterator<Item = &mut U>
// fn into_iter(self) -> IntoIter // IntoIter implements Iterator<Item = U>Fiter
//
// 惯例:
// new: 构造器, 参数 no self, >= 1
// with_...: 可选构造器, 参数 no self, >= 1
// from_...: 转换接口, 参数 >=1
// into_...: 可能昂贵转换接口, 参数 self
// as_...: 轻松转换接口, 参数&self
// to_...: 昂贵转换接口, 参数&self
// is_...: 判断, 返回bool, 参数 &self or none
// has_...: 判断, 返回bool, 参数 &self or none

fn main() {
// if-else
Expand Down
116 changes: 54 additions & 62 deletions doc/宏编程.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
// macro_rules! $name {
// ($arg: ty) => {$expansion};
// ...
// }
// $arg: ty
// ident: an identifier
// ty: a type
// expr: an expression
// item: 语言项, like a function, struct, module, etc.
// block: 代码块, surrounded by braces
// stmt: 语句, surrounded by semicolons
// pat: a pattern
// path: a path (e.g. foo, ::std::mem::replace, transmute::<_, int>, …)
// meta: 元信息 the things that go inside #[...] and #![...] attributes
// tt: a single token tree
// vis: 可见性, pub
// lifetime: 生命周期
// Repetitions
// $ ( ... ) sep rep
// sep is an optional separator token. Common examples are , and ;
// rep is the required repeat control. * (indicating zero or more repeats) or + (indicating one or more repeats)
// 允许最后多一个逗号:
// ($($exprs:expr),* $(,)*) => {...};
// Debug
// #[feature(trace_macros)]
// trace_macros!(true);
// cargo expand
// 重复
macro_rules! vec_strs {
(
// Start a repetition:
$(
// Each repeat must contain an expression...
$element:expr
)
// ...separated by commas...
,
// ...zero or more times.
*
) => {
// Enclose the expansion in a block so that we can use
// multiple statements.
{
let mut v = Vec::new();

// Start a repetition:
$(
// Each repeat will contain the following statement, with
// $element replaced with the corresponding expression.
v.push(format!("{}", $element));
)*

v
}
};
}
// 递归回调宏
macro_rules! invoke_with_callback {
( $callback:ident($($args:tt)* )) => {
$callback!($($args)*)
};
}
// 普通宏: 简单AST替换
// macro_rules! $name {
// ($arg: ty) => {$expansion};
// ...
// }
// $arg: ty
// ident: 标识符, 变量名称
// ty: 类型
// expr: 表达式
// item: 语言项, like a function, struct, module, etc.
// block: 代码块, surrounded by braces
// stmt: 语句, surrounded by semicolons
// pat: a pattern
// path: a path (e.g. foo, ::std::mem::replace, transmute::<_, int>, …)
// meta: 元信息 the things that go inside #[...] and #![...] attributes
// tt: a single token tree
// vis: 可见性, pub
// lifetime: 生命周期
// Repetitions
// $ ( ... ) sep rep
// sep is an optional separator token. Common examples are , and ;
// rep is the required repeat control. * (indicating zero or more repeats) or + (indicating one or more repeats)
// 允许最后多一个逗号:
// ($($exprs:expr),* $(,)*) => {...};
// Debug
// #[feature(trace_macros)]
// trace_macros!(true);
// cargo expand
// // 递归回调宏
// macro_rules! invoke_with_callback {
// ( $callback:ident($($args:tt)* )) => {
// $callback!($($args)*)
// };
// }
// 过程宏: AST ==> AST
// proc-macro库
// ```toml
// [lib]
// proc-macro = true
// ```
// 辅助框架: quote、syn
// 分类:
// 函数式
// 使用公有函数 pub fn 声明, 函数名即宏名
// #[proc_macro]
// 函数签名为 (TokenStream) -> TokenStream
// derive 式
// 使用公有函数 pub fn 声明
// #[proc_macro_derive(Name)] #[proc_macro_derive(Name, attributes(attr))]
// 函数签名为 (TokenStream) -> TokenStream
// 属性式
// 使用公有函数 pub fn 声明,函数名为属性名
// #[proc_macro_attribute]
// 函数签名为 (TokenStream, TokenStream) -> TokenStream
12 changes: 0 additions & 12 deletions doc/常用框架/anyhow.rs

This file was deleted.

14 changes: 0 additions & 14 deletions doc/常用框架/async-std.rs

This file was deleted.

3 changes: 3 additions & 0 deletions doc/常用框架/async/async-std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
*
*/
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 57f08b2

Please sign in to comment.