repo
stringlengths
6
65
file_url
stringlengths
81
311
file_path
stringlengths
6
227
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:31:58
2026-01-04 20:25:31
truncated
bool
2 classes
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/linear_data_structure/src/bin/queue.rs
数据结构和算法/linear_data_structure/src/bin/queue.rs
//!实现队列 #[derive(Debug)] pub struct Queue<T> { cap: usize, datas: Vec<T>, } impl<T> Queue<T> { pub fn new(size: usize) -> Self { Self { cap: size, datas: Vec::with_capacity(size), } } // 判断队列元素个数 pub fn len(&self) -> usize { self.datas.len() }...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/linear_data_structure/src/bin/use_dequeue_palindrome_checker.rs
数据结构和算法/linear_data_structure/src/bin/use_dequeue_palindrome_checker.rs
//!使用双端队列进行回文检测 use dequeue::Dequeue; mod dequeue; // 检测字符串是否是回文字符串 fn check_palindrome(input: &str) -> bool { let mut dq = Dequeue::new(input.len()); for c in input.chars() { let _ = dq.add_rear(c); } let mut is_match = true; while dq.len() > 1 && is_match { let head = dq.remove_...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/consistency_hash/src/bin/consistency_hash.rs
数据结构和算法/consistency_hash/src/bin/consistency_hash.rs
//! 一致性hash算法 use std::{ collections::{hash_map::DefaultHasher, BTreeMap}, fmt::Debug, hash::{Hash, Hasher}, }; // 主机节点 #[derive(Debug, Clone)] struct MachineNode { host: &'static str, ip: &'static str, port: u16, } // 为MachineNode添加字符串转换 impl ToString for MachineNode { fn to_string(&self)...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/lru/src/bin/lru.rs
数据结构和算法/lru/src/bin/lru.rs
//! LRU 最近最少使用缓存淘汰算法 //! 基于LRUList数据结构:双向链表且头节点指向尾节点 use std::{collections::HashMap, mem::swap}; // 可变裸指针,方便通过裸指针修改 type LRUHandle<T> = *mut LRUNode<T>; #[derive(Debug)] struct LRUNode<T> { next: Option<Box<LRUNode<T>>>, prev: Option<LRUHandle<T>>, data: Option<T>, } #[derive(Debug)] struct LRUList<T> {...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/heap_sort.rs
数据结构和算法/sort_algorithms/src/bin/heap_sort.rs
//! 堆排序 //! 平均复杂度O(nlog2n) 空间复杂度O(1) 不稳定 /// 计算父节点下标 fn parent(child_index: usize) -> usize { child_index >> 1 } /// 计算左子节点的下标 fn left_child(parent_index: usize) -> usize { parent_index << 1 } /// 计算右子节点的下标 fn right_child(parent_index: usize) -> usize { (parent_index << 1) + 1 } pub fn heap_sort(nums: &mu...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/counting_sort.rs
数据结构和算法/sort_algorithms/src/bin/counting_sort.rs
//! 计数排序 //! 平均时间复杂度O(n+k) 空间复杂度O(n+k) 稳定 n为待排数组长度,k为待排数据个数 pub fn counting_sort(nums: &mut [usize]) { if nums.len() <= 1 { return; } // 桶的数量等于最大值-最小值+1 let max = nums.iter().max().unwrap().clone(); let min = nums.iter().min().unwrap().clone(); let bkt_nums = max - min + 1; // 将数...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/select_sort.rs
数据结构和算法/sort_algorithms/src/bin/select_sort.rs
//! 选择排序 //! 平均时间复杂度O(n^2) 空间复杂度 O(1) 不稳定 pub fn select_sort(nums: &mut [i32]) { let mut left = nums.len() - 1; while left > 0 { let mut pos_max = 0; for i in 1..=left { if nums[i] > nums[pos_max] { pos_max = i; } } // 交换数据,完成一轮数据的排序,并将待排序...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/radix_sort.rs
数据结构和算法/sort_algorithms/src/bin/radix_sort.rs
//! 基数排序 //! 平均时间复杂度O(O*K) 空间复杂度O(n+k) 稳定 pub fn radix_sort(nums: &mut [usize]) { if nums.len() <= 1 { return; } // 最大值 let max_num = nums.iter().max().unwrap().clone(); // 寻找最小的x,使得2^x>= len let radix = nums.len().next_power_of_two(); let mut digit = 1; while digit <= max_num...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/compare_test.rs
数据结构和算法/sort_algorithms/src/bin/compare_test.rs
use std::time::{Duration, Instant}; use bubbule_sort::bubbule_sort; use shell_sort::shell_sort; use quick_sort::quick_sort; use insertion_sort::insert_sort; use merge_sort::merge_sort; use select_sort::select_sort; use heap_sort::heap_sort; use counting_sort::counting_sort; use bucket_sort::bucket_sort; use radix_sort...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/merge_sort.rs
数据结构和算法/sort_algorithms/src/bin/merge_sort.rs
//! 归并排序 递归+合并 //! 平均时间复杂度O(nlog(2n)) 空间复杂度O(n) 稳定 pub fn merge_sort(nums: &mut [i32]) { if nums.len() > 1 { let mid = nums.len() >> 1; // 排序前半部分数据 merge_sort(&mut nums[..mid]); // 排序后半部分数据 merge_sort(&mut nums[mid..]); merge(nums, mid); } } fn merge(nums: &mut...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/insertion_sort.rs
数据结构和算法/sort_algorithms/src/bin/insertion_sort.rs
//! 插入排序算法 //! 平均时间复杂度O(n^2) 空间复杂度O(1) 稳定 pub fn insert_sort(nums: &mut [i32]) { if nums.len() <= 1 { return; } for i in 1..nums.len() { let mut pos = i; let current = nums[i]; while pos > 0 && current < nums[pos - 1] { nums[pos] = nums[pos - 1]; pos ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/bubbule_sort.rs
数据结构和算法/sort_algorithms/src/bin/bubbule_sort.rs
//! 排序算法之冒泡排序 //! 平均时间复杂度为O(n^2) 空间复杂度O(1) 稳定 pub fn bubbule_sort(nums: &mut [i32]) { if nums.len() <= 1 { return; } else { for i in 1..nums.len() { for j in 0..nums.len() - i { // 如果j大于j+1元素,则交换 if nums[j] > nums[j + 1] { nums.swa...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/quick_sort.rs
数据结构和算法/sort_algorithms/src/bin/quick_sort.rs
//! 快速排序 //! 平均时间复杂度O(nlog2N) 空间复杂度O(nlog2N) 不稳定 pub fn quick_sort(nums: &mut [i32], low: usize, high: usize) { if low < high { // 选出分裂点 let split_index = partition(nums, low, high); if split_index > 1 { // 对分裂点左边排序 quick_sort(nums, low, split_index - 1); } ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/bucket_sort.rs
数据结构和算法/sort_algorithms/src/bin/bucket_sort.rs
//! 桶排序 //! 平均时间复杂度O(n+k),其中k是桶个数 空间复杂度O(n+k) 稳定 use std::fmt::Debug; struct Bucket<H, T> { hash: H, values: Vec<T>, } impl<H, T> Bucket<H, T> { fn new(hash: H, value: T) -> Self { Bucket { hash: hash, values: vec![value], } } } pub fn bucket_sort<H, T, F>(num...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/sort_algorithms/src/bin/shell_sort.rs
数据结构和算法/sort_algorithms/src/bin/shell_sort.rs
//! 希尔排序 //! 平均时间复杂度O(n^1.3) 空间复杂度O(1) 不稳定 pub fn shell_sort(nums: &mut [i32]) { // 内部是插入排序 fn insert_sort(nums: &mut [i32], start: usize, gap: usize) { let mut i = start + gap; while i < nums.len() { let mut pos = i; let current = nums[pos]; while pos >= ga...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/filter_algorithms/src/bitset.rs
数据结构和算法/filter_algorithms/src/bitset.rs
//! 位图 #[derive(Debug)] pub struct BitSet { length: usize, bits: Vec<u64>, } // 对齐 const ALIGN: usize = 6; // 字大小(2^6 = 64) 一个u64存储64位 const WORD_SIZE: usize = 64; // vec的大小 fn vec_size(n: usize) -> usize { let mut size = 0; if n & (WORD_SIZE - 1) == 0 { size = n >> ALIGN; } size = (n...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/filter_algorithms/src/lib.rs
数据结构和算法/filter_algorithms/src/lib.rs
pub mod bitset; pub mod bloom_filter;
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/filter_algorithms/src/bloom_filter.rs
数据结构和算法/filter_algorithms/src/bloom_filter.rs
//! 使用hash函数+BitSet实现 布隆过滤器 use std::{ collections::hash_map::DefaultHasher, hash::{Hash, Hasher}, }; #[path = "bitset.rs"] mod bitset; use bitset::BitSet; pub struct BloomFilter { pub k: usize, // 哈希函数的个数 pub bits: BitSet, } // 根据key,产生5个哈希值 fn hash<T: Hash + ?Sized>(key: &T) -> [u64; 5] { let...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/filter_algorithms/src/bin/run_bloom_filter.rs
数据结构和算法/filter_algorithms/src/bin/run_bloom_filter.rs
#[path = "../bloom_filter.rs"] mod bloom_filter; use bloom_filter::BloomFilter; fn main() { let mut b = BloomFilter::new(1024, 3); println!("bitset len:{}", b.bits.len()); b.add(&999); b.add(&10); b.add("你好"); b.add(&345); assert!(b.test(&999)); assert!(b.test(&10)); assert!(b.tes...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/search_algorithms/src/bin/hash_search.rs
数据结构和算法/search_algorithms/src/bin/hash_search.rs
//! 哈希查找 //! hash函数:一种值到存储地址的映射函数,通过它直接建立值和地址的关系,达到快速查找的目的 //! hash 冲突的解决:拉链法,在冲突的槽位建立链表(或一颗红黑树),此时时间复杂度为O(log2N) use std::{ collections::hash_map::DefaultHasher, fmt::Debug, hash::{Hash, Hasher}, }; #[derive(Debug, Clone, PartialEq, PartialOrd)] struct HashMap<K, V> { // 分配的容量 cap: usize, // ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/search_algorithms/src/bin/binary_search.rs
数据结构和算法/search_algorithms/src/bin/binary_search.rs
//! 二分查找 //! 时间复杂度O(log2n) //! 前提条件:需要集合有序 /// 二分查找有序数组 fn binary_search<T: PartialEq + PartialOrd>(nums: &[T], num: T) -> bool { let mut found = false; let mut low = 0; let mut high = nums.len() - 1; while low <= high && !found { // 右移操作,mid为len的一半 let mid = (low + high) >> 1; ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/search_algorithms/src/bin/sequential_search.rs
数据结构和算法/search_algorithms/src/bin/sequential_search.rs
//! 顺序查找 //! 时间复杂度O(n) fn sequential_search<T: PartialEq>(nums: &[T], num: T) -> bool { let mut found = false; let mut index = 0; while index < nums.len() { if num == nums[index] { found = true; break; } index += 1; } found } fn main() { let num = ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/search_algorithms/src/bin/middle_insert_search.rs
数据结构和算法/search_algorithms/src/bin/middle_insert_search.rs
//! 内插查找 //! 它是二分查找的变体,思路是使用线性内插法 (y-y0)/(x-x0) = (y1-y0)/(x1-x0) => x = {(y-y0)(x1-x0)}/(y1-y0) + x0 //! 时间平均查找复杂度为O(loglog(n)) //! 前提条件:数组已经排好序 fn insert_search(nums: &[i32], target: i32) -> bool { if nums.is_empty() { return false; } let mut low = 0; let mut high = nums.len() - 1; // 一直...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/tree_structures_and_algos/src/bin/tire.rs
数据结构和算法/tree_structures_and_algos/src/bin/tire.rs
//! 字典树 #[derive(Default, Debug)] struct Trie { root: Box<Node>, // 字典树的根节点 } #[derive(Default, Debug)] struct Node { mark: bool, // 标识位:用于标识是否从根节点到此节点组成一个单词 childs: [Option<Box<Node>>; 26], //26个英文字母,只需要26个槽位 } impl Trie { fn new() -> Self { Self::default() } //...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/postfix_expression_cal/src/stack.rs
数据结构和算法/postfix_expression_cal/src/stack.rs
//! 实现栈数据结构 #[derive(Debug)] pub struct Stack<T> { size: usize, // 栈大小 data: Vec<T>, // 栈数据,泛型T } impl<T> Stack<T> { // 初始化空栈 pub fn new() -> Self { Self { size: 0, data: vec![], } } // 判空 pub fn is_empty(&self) -> bool { self.size == 0 ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/postfix_expression_cal/src/par_checker.rs
数据结构和算法/postfix_expression_cal/src/par_checker.rs
use crate::stack::Stack; use std::vec; /// 判断是否匹配 fn matcher(left: char, right: char) -> bool { let lefts = "([{"; let rights = ")]}"; lefts.find(left) == rights.find(right) } // 使用栈判断括号是否匹配 pub fn par_checher(input: &str) -> bool { if input.is_empty() { return false; } else { let m...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/数据结构和算法/postfix_expression_cal/src/main.rs
数据结构和算法/postfix_expression_cal/src/main.rs
//! 后缀表达式计算 use std::collections::HashMap; use par_checker::par_checher; use stack::Stack; mod par_checker; mod stack; /// 中缀表达式转换为后缀表达式 fn infix_to_postfix(infix: &str) -> Option<String> { // 1.括号检测 if !par_checher(infix) { return None; } // 2.设置运算符优先级 let mut prec = HashMap::new(); ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/生命周期/src/bin/lifecycle.rs
生命周期/src/bin/lifecycle.rs
fn main() { // 悬垂指针 // let age; // { // let age1 = 30; // age = &age1; // } // println!("age:{age}"); // 生命周期标注语法 // 显式周期的引用 &'a u32 // 显式周期的可变引用 &'a mut u32 // 函数传参,显式标明生命周期 // 类似于泛型,要在尖括号中先声明才能使用 // 第一个参数是生命周期为'a的不可变引用 // 第二个参数是生命周期为'a的可变引用 fn bige...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_rustyline.rs
好用的第三方包/taste_crates/src/bin/taste_rustyline.rs
use rustyline::error::ReadlineError; use rustyline::{DefaultEditor, Result}; fn main() -> Result<()> { // `()` can be used when no completer is required let mut rl = DefaultEditor::new()?; #[cfg(feature = "with-file-history")] if rl.load_history("history.txt").is_err() { println!("No previous h...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_bigdecimal.rs
好用的第三方包/taste_crates/src/bin/taste_bigdecimal.rs
use bigdecimal::BigDecimal; fn main() { let two = BigDecimal::from(2); println!("sqrt(2) = {}", two.sqrt().unwrap()); let three = BigDecimal::from(3); println!("sqrt(3) = {}", three.sqrt().unwrap()); let four = BigDecimal::from(4); println!("sqrt(4) = {}", four.sqrt().unwrap()); }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_thiserror.rs
好用的第三方包/taste_crates/src/bin/taste_thiserror.rs
use std::io; use thiserror::Error as ThisError; fn main() { self_error_type(); self_error_type1(); self_error_type2(); } // 动态错误类型 fn self_error_type2() { // 自定义错误类型的定义 #[derive(ThisError, Debug)] pub enum MyError { // FailedWithCode 的错误描述,其中 {0} 会被动态地替换为具体的代码值 #[error("failed ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_seder.rs
好用的第三方包/taste_crates/src/bin/taste_seder.rs
use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct Point { x: i32, y: i32, } fn main() { let point = Point { x: 1, y: 2 }; // Convert the Point to a JSON string. let serialized = serde_json::to_string(&point).unwrap(); // Prints serialized = {"x":1,"y":2} ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_miette.rs
好用的第三方包/taste_crates/src/bin/taste_miette.rs
use miette::{Diagnostic, SourceSpan}; use miette::{NamedSource, Result as MietteResult}; use std::error::Error; use thiserror::Error; #[derive(Error, Debug, Diagnostic)] #[error("oops!")] #[diagnostic(code(src::bin::taste_miette), url(docsrs), help("请认真检查你的代码"))] struct MyBad { // The Source that we're gonna be pri...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_tabled1.rs
好用的第三方包/taste_crates/src/bin/taste_tabled1.rs
use tabled::{ settings::{object::Rows, Alignment, Modify, Style}, Table, Tabled, }; #[derive(Debug, Tabled)] struct Distribution { name: String, based_on: String, is_active: bool, } impl Distribution { fn new(name: &str, base: &str, is_active: bool) -> Self { Self { based_o...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_derive_more.rs
好用的第三方包/taste_crates/src/bin/taste_derive_more.rs
use derive_more::{ Add, AddAssign, Constructor, Deref, DerefMut, Display, From, FromStr, Index, IndexMut, Into, IsVariant, Mul, MulAssign, Not, TryInto, }; /// Some docs #[derive( Add, AddAssign, Constructor, Display, From, FromStr, Into, Mul, MulAssign, Not, Clone, Copy, )] pub struct MyInt(i32); /// Som...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/好用的第三方包/taste_crates/src/bin/taste_tabled2.rs
好用的第三方包/taste_crates/src/bin/taste_tabled2.rs
use tabled::{ settings::{style::Style, themes::Theme, Color}, Table, Tabled, }; #[derive(Tabled)] struct CodeEditor { name: &'static str, first_release: &'static str, developer: &'static str, } impl CodeEditor { fn new(name: &'static str, first_release: &'static str, developer: &'static str) -...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/bridge/src/bin/bridge.rs
设计模式/bridge/src/bin/bridge.rs
//! 桥接模式 // 定义实现接口 trait Implementor { fn op_impl(&self); } // 定义具体的实现类 struct ConcreteImplA; impl Implementor for ConcreteImplA { fn op_impl(&self) { println!("实现类ConcreteImplA op....") } } struct ConcreteImplB; impl Implementor for ConcreteImplB { fn op_impl(&self) { println!("实现类Conc...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/filter/src/bin/filter.rs
设计模式/filter/src/bin/filter.rs
//! 过滤器模式 // 需要过滤的结构体 #[derive(PartialEq, Clone)] struct Person { name: String, gender: String, marital_status: String, } impl Person { fn get_gender(&self) -> &str { self.gender.as_str() } fn get_marital_status(&self) -> &str { self.marital_status.as_str() } fn get_name...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/state/src/bin/state.rs
设计模式/state/src/bin/state.rs
// 定义状态,不同的操作会影响状态,同时状态的改变又会影响对象和流程 trait State { fn handle(&self, context: &mut Context); fn get_temperature(&self) -> f32; } struct Context { state: Option<Box<dyn State>>, } impl Context { fn set_state(&mut self, state: Box<dyn State>) { self.state = Some(state); } fn request(&mut s...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/visitor/src/bin/visitor.rs
设计模式/visitor/src/bin/visitor.rs
trait Visitor { fn visit_element_a(&self, element: &ElementA); fn visit_element_b(&self, element: &ElementB); } trait Element { fn accept(&self, viritor: &dyn Visitor); } #[derive(Debug)] struct ElementA { name: String, age: i32, } impl Element for ElementA { fn accept(&self, viritor: &dyn Visit...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/proxy/src/bin/proxy.rs
设计模式/proxy/src/bin/proxy.rs
trait File { fn read(&self); } struct TxtFile { file_name: String, } impl TxtFile { fn load_from_dist(&self) { println!("加载文件:{}", &self.file_name); } fn new(file_name: String) -> Self { let file = TxtFile { file_name }; file.load_from_dist(); file } } impl File...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/composite/src/bin/composite.rs
设计模式/composite/src/bin/composite.rs
//! 组合模式 // 组件接口 trait Component{ fn name(&self)->&'static str; fn search(&self,keyword:&str); } // 文件(Leaf) struct File{ name:&'static str } impl File{ fn new(name:&'static str)->Self{ Self{name} } } impl Component for File{ fn name(&self)->&'static str { self.name } f...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/builder/src/bin/builder.rs
设计模式/builder/src/bin/builder.rs
//! 实现构建者模式 // 使用构建者模式模拟食堂制备早餐 // 早餐包含的食物,抽象为Item trait Item { fn name(&self) -> String; fn price(&self) -> f32; fn packing(&self) -> String; } // 鸡肉汉堡 struct ChickenHamburg; impl Item for ChickenHamburg { fn name(&self) -> String { String::from("鸡肉汉堡") } fn price(&self) -> f32 { ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/command/src/bin/command.rs
设计模式/command/src/bin/command.rs
use std::{collections::HashMap, rc::Rc}; // 电视 Receiver struct Tv; impl Tv { fn on(&self) { println!("打开电视....") } fn off(&self) { println!("关闭电视....") } } // 命令接口 trait Command { fn execute(&self); } // 具体命令:打开电视 struct OnTvCommand { tv: Rc<Tv>, } impl OnTvCommand { fn ne...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/observer/src/main.rs
设计模式/observer/src/main.rs
use std::collections::HashMap; trait Observer { fn update(&mut self, temperature: f32, pressure: f32, humidity: f32); fn display(&self); fn get_id(&self) -> i32; } struct Baidu { id: i32, temperature: f32, pressure: f32, humidity: f32, } impl Observer for Baidu { fn update(&mut self, t...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/singleton/src/main.rs
设计模式/singleton/src/main.rs
fn main() { println!("Hello, world!"); }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/singleton/src/bin/sync_once_singleton.rs
设计模式/singleton/src/bin/sync_once_singleton.rs
use std::cell::RefCell; use std::sync::{Arc, Once}; use std::{thread, vec}; type V = Option<Arc<RefCell<Vec<i32>>>>; static mut VALS: V = None; static INIT: Once = Once::new(); fn main() { let handle1 = thread::spawn(move || { INIT.call_once(|| unsafe { VALS = Some(Arc::new(RefCell::new(vec![1, ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/singleton/src/bin/static_mutex_singleton.rs
设计模式/singleton/src/bin/static_mutex_singleton.rs
use std::{ sync::{Arc, Mutex}, thread, time::Duration, }; #[derive(Debug)] struct Singleton(Vec<i32>); static mut INSTANCE: Option<Arc<Mutex<Singleton>>> = None; //静态初始化,只运行一次 static LOCK: Mutex<i32> = Mutex::new(0); impl Singleton { //关联方法, 获取单例实例的方法 fn get_instance(sec: u64) -> Arc<Mutex<Singleto...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/singleton/src/bin/lazy_static_singleton.rs
设计模式/singleton/src/bin/lazy_static_singleton.rs
use lazy_static::lazy_static; use std::{sync::Mutex, thread}; lazy_static! { pub static ref ARRAY: Mutex<Vec<u8>> = Mutex::new(Vec::new()); } pub fn do_a_call() { // 验证内存地址 println!("addr:{:p}", &*ARRAY.lock().unwrap()); ARRAY.lock().unwrap().push(1); } fn main() { // 获取单例实例,自定义 let handle1 =...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/iterator/src/bin/stack.rs
设计模式/iterator/src/bin/stack.rs
//! 实现栈数据结构并为栈实现三种不同类型的迭代器 #[derive(Debug)] struct Stack<T> { size: usize, data: Vec<T>, } impl<T> Stack<T> { // 初始化栈 fn new() -> Self { Self { size: 0, data: vec![], } } // 判断栈是否为空 fn is_empty(&self) -> bool { self.size == 0 } // 栈的长度...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/iterator/src/bin/num_generator.rs
设计模式/iterator/src/bin/num_generator.rs
//! 基于Iterator实现数字生成器 use std::{collections::HashSet, ops::AddAssign}; pub struct NumberGenerator { // 开始 begin: usize, // 结束 end: usize, } impl NumberGenerator { pub fn new(begin: usize, end: usize) -> Self { Self { begin, end } } } impl Iterator for NumberGenerator { type Item = u...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/chain_of_responsibility/src/bin/chain_of_responsibility.rs
设计模式/chain_of_responsibility/src/bin/chain_of_responsibility.rs
//! 责任链模式 //! // 定义Requst结构体抽象为Patient病人 struct Patient { name: String, } // 处理者Handler抽象为Department部门 trait Department { // 处理者接口 fn handle(&mut self, patient: &mut Patient); // 责任链的下一级 fn next(&mut self) -> &mut Option<Box<dyn Department>>; fn execute(&mut self, patient: &mut Patient) { ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/decorator/src/bin/decorator.rs
设计模式/decorator/src/bin/decorator.rs
// 先定义component,这里抽象为DataSource trait DataSource { fn write(&mut self); fn read(&self); } // 具体的component,这里为FileDataSource #[derive(Default)] struct FileDataSource; impl DataSource for FileDataSource { fn write(&mut self) { println!("文件数据写入...") } fn read(&self) { println!("文件数据读取....
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/decorator/src/bin/std_io.rs
设计模式/decorator/src/bin/std_io.rs
use std::io::{BufReader, Cursor, Read}; fn main() { // 标准库中的io模块使用了装饰器模式 let mut input = BufReader::new(Cursor::new("study rust")); let mut buf = [0; 20]; input.read(&mut buf).ok(); for byte in buf { print!("{}", char::from(byte)); } }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/template_method/src/bin/template_method.rs
设计模式/template_method/src/bin/template_method.rs
trait Template { // 抽象方法 fn step1(&self); // 默认方法 fn step2(&self) { println!("step2:去掉皮") } // 抽象方法 fn step3(&self); // 默认方法 fn step4(&self) { println!("step4:煮耙后出锅") } // 模板方法 fn action(&self) { self.step1(); self.step2(); self.ste...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/facade/src/bin/facade.rs
设计模式/facade/src/bin/facade.rs
// 零件 trait Part { fn run(&self); } // 喇叭 struct Horn; // 屏幕 struct Screen; // 网络 struct Net; // 变压器 struct Transformer; impl Part for Horn { fn run(&self) { println!("喇叭已就位...") } } impl Part for Screen { fn run(&self) { println!("屏幕已就位...") } } impl Part for Net { fn run(&sel...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/mediator/src/bin/mediator.rs
设计模式/mediator/src/bin/mediator.rs
use std::collections::HashMap; trait Mediator { fn register_colleague(&mut self, member: Box<dyn Colleague>); fn notify(&self, member_name: &str, msg: String); } struct CenteralMediator { // 注册的同事名字-同事对象的对应关系 members: HashMap<String, Box<dyn Colleague>>, } impl Mediator for CenteralMediator { fn re...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/prototype/src/bin/prototype.rs
设计模式/prototype/src/bin/prototype.rs
#[derive(Clone, PartialEq, Eq, Debug)] struct Shape { id: String, mtype: String, } impl Shape { fn set_id(&mut self, id: String) { self.id = id; } } #[derive(Clone, PartialEq, Eq, Debug)] struct Circle { shape: Shape, } impl Circle { fn new() -> Circle { Circle { sha...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/factory_method_and_absfactory/src/main.rs
设计模式/factory_method_and_absfactory/src/main.rs
// 工厂模式和抽象工厂模式 fn main() { println!("Hello, world!"); }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/factory_method_and_absfactory/src/bin/simple_factory.rs
设计模式/factory_method_and_absfactory/src/bin/simple_factory.rs
trait Product { fn weight(&self) -> f64; } #[derive(Debug)] struct Pen; #[derive(Debug)] struct Car; impl Product for Pen { fn weight(&self) -> f64 { println!("这只笔重量为150g"); 150.0 } } impl Product for Car { fn weight(&self) -> f64 { println!("这辆汽车重1.5吨"); 1500000.0 ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/factory_method_and_absfactory/src/bin/factory_method.rs
设计模式/factory_method_and_absfactory/src/bin/factory_method.rs
trait Product { fn weight(&self) -> f64; } trait Factory { type ProductType; fn create_product(&self) -> Self::ProductType; } struct Pen; impl Product for Pen { fn weight(&self) -> f64 { println!("这只笔重量为150g"); 150.0 } } struct PenFactory; impl Factory for PenFactory { type Pro...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/factory_method_and_absfactory/src/bin/abs_factory.rs
设计模式/factory_method_and_absfactory/src/bin/abs_factory.rs
trait Engine { fn show(&self); } trait Wheel { fn show(&self); } trait Factory { type E: Engine; type W: Wheel; // 返回具体的关联类型 fn create_engine(&self) -> Self::E; fn create_wheel(&self) -> Self::W; } trait DynFactory { // 返回特征对象,更具有通用性 fn dyn_create_engine(&self) -> Box<dyn Engine>; ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/memento/src/bin/memento.rs
设计模式/memento/src/bin/memento.rs
//! 备忘录模式 // 备忘录 #[derive(Clone)] struct Memento { state: String, } impl Memento { fn get_state(&self) -> &str { &self.state } fn set_state(&mut self, state: String) { self.state = state } } // 发起人 struct Originator { state: String, } impl Originator { fn get_state(&self) -...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/adapter/src/bin/has_a_adapter.rs
设计模式/adapter/src/bin/has_a_adapter.rs
trait Target { fn request(&self) -> String; } pub struct Adaptee { value: i32, } impl Adaptee { fn specific_request(&self) -> String { format!("Adaptee value is {}", self.value) } } struct Adapter { adaptee: Adaptee, } impl Adapter { fn new(adaptee: Adaptee) -> Self { Adapter ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/strategy/src/main.rs
设计模式/strategy/src/main.rs
trait Strategy { fn do_operation(&self, num1: i32, num2: i32) -> i32; } struct StrategyAdd; impl Strategy for StrategyAdd { fn do_operation(&self, num1: i32, num2: i32) -> i32 { num1 + num2 } } struct StrategySub; impl Strategy for StrategySub { fn do_operation(&self, num1: i32, num2: i32) -> i...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/interpreter/src/bin/interpreter.rs
设计模式/interpreter/src/bin/interpreter.rs
//! 解释器模式 trait Expression { fn interpret(&self) -> i32; } // 数字表达式 属于TerminalExpression struct NumberExpression { value: i32, } impl NumberExpression { fn new(value: i32) -> Self { NumberExpression { value } } } impl Expression for NumberExpression { fn interpret(&self) -> i32 { s...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/设计模式/flyweight/src/bin/flyweight.rs
设计模式/flyweight/src/bin/flyweight.rs
use std::{collections::HashMap, sync::Arc}; #[warn(dead_code)] struct BigObject { data: [i32; 10000], } impl Default for BigObject { fn default() -> Self { Self { data: [0; 10000] } } } struct FlywightFactory { // BigObject需要共享,使用Arc只能指针 objects: HashMap<String, Arc<BigObject>>, } impl Def...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/mvcc/src/main.rs
手撸系列/write_a_db/mvcc/src/main.rs
use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, HashMap, HashSet}, sync::{ atomic::{AtomicU64, Ordering}, Arc, Mutex, }, }; // 存储引擎定义,这里使用一个简单的内存 BTreeMap pub type KVEngine = BTreeMap<Vec<u8>, Option<Vec<u8>>>; // 全局递增的版本号 static VERSIO...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/services/arepl/src/main.rs
手撸系列/write_a_db/adb/crates/services/arepl/src/main.rs
use aparser::{ ast::{parse_sql_command, SqlCommand}, error::FormattedError, }; use miette::{Context, GraphicalReportHandler, IntoDiagnostic}; use rustyline::{ completion::FilenameCompleter, error::ReadlineError, highlight::Highlighter, Completer, CompletionType, Config, Editor, Helper, Hinter, Validator...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aexecution/src/main.rs
手撸系列/write_a_db/adb/crates/libs/aexecution/src/main.rs
fn main() { println!("Hello, world!"); }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/ast.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/ast.rs
//! 将渐变语句、查询语句、插入语句整合起来 use crate::{ commands::{CreateStatement, InsertStatement, SelectStatement}, error::FormattedError, parse::{peek_then_cut, Parse}, }; use nom::{ branch::alt, character::complete::{char, multispace0}, combinator::{eof, map}, error::context, multi::many1, sequen...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/lib.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/lib.rs
pub mod ast; pub mod commands; pub mod error; pub mod parse; pub mod parse_value;
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/parse.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/parse.rs
//! 定义通用的解析方法 use nom::{ self, bytes::complete::{tag_no_case, take_while1}, character::complete::{char, multispace0}, combinator::{all_consuming, map, peek}, multi::separated_list1, sequence::{pair, tuple}, Finish, IResult, }; use nom_locate::LocatedSpan; use crate::error::{format_parse_er...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/error.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/error.rs
//! 统一错误错误 //! use miette::Diagnostic; use nom_supreme::error::{BaseErrorKind, ErrorTree, GenericErrorTree, StackContext}; use thiserror::Error; use crate::parse::RawSpan; // 定义错误类型 pub type MyParseError<'a> = ErrorTree<RawSpan<'a>>; // 定义miette断言的结构体 #[derive(Error, Debug, Diagnostic)] #[error("解析错误")] pub struct F...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/parse_value.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/parse_value.rs
//! 解析值 use std::str::FromStr; use crate::parse::{peek_then_cut, Parse, ParseResult, RawSpan}; use bigdecimal::BigDecimal; use derive_more::Display; use nom::{ branch::alt, bytes::complete::{tag, take_until, take_while}, character::complete::multispace0, error::context, sequence::{preceded, termin...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/select.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/select.rs
//! 查询命令解析 use crate::parse::{comma_sep, identifier, Parse}; use nom::{ bytes::complete::tag_no_case, character::complete::multispace1, error::context, sequence::tuple, }; use nom_supreme::ParserExt; use serde::{Deserialize, Serialize}; /// 查询命令结构体 #[derive(Debug, Clone, PartialEq, Default, Hash, Serialize, Deser...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/insert.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/insert.rs
//! 插入语句解析 use crate::{ parse::{comma_sep, identifier, Parse}, parse_value::Value, }; use nom::{ bytes::complete::tag_no_case, character::complete::multispace1, error::context, sequence::{preceded, tuple}, }; use nom_supreme::ParserExt; use serde::{Deserialize, Serialize}; /// 插入语句结构体 #[derive...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/mod.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/mod.rs
mod create; mod insert; mod select; pub use create::CreateStatement; pub use insert::InsertStatement; pub use select::SelectStatement;
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/create.rs
手撸系列/write_a_db/adb/crates/libs/aparser/src/commands/create.rs
//! create sql解析 use crate::parse::{comma_sep, identifier, Parse, ParseResult, RawSpan}; use derive_more::Display; use nom::{ branch::alt, bytes::complete::tag_no_case, character::{ complete::{char, multispace1}, streaming::multispace0, }, combinator::map, error::context, se...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/mini_async_runtime/src/bin/mini-async-runtime.rs
手撸系列/mini_async_runtime/src/bin/mini-async-runtime.rs
use { futures::{ future::{BoxFuture, FutureExt}, task::{waker_ref, ArcWake}, }, std::{ future::Future, pin::Pin, sync::{ mpsc::{self, Receiver, Sender}, Arc, Mutex, }, task::{Context, Poll, Waker}, thread, time::...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/mini_blockchain/src/bin/mini_blockchain.rs
手撸系列/mini_blockchain/src/bin/mini_blockchain.rs
//! 迷你区块链 use chrono::Utc; use serde::Serialize; use sha2::{Digest, Sha256}; use std::{thread, time::Duration}; /// 区块头 #[derive(Serialize, Debug, PartialEq, Eq)] struct BlockHeader { pre_hash: String, txs_hash: String, time: i64, } /// 区块体 #[derive(Debug)] struct Block { // 区块头 header: BlockHead...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/mini_kvm_sample/kvmsample-rust/src/main.rs
手撸系列/mini_kvm_sample/kvmsample-rust/src/main.rs
extern crate kvm_ioctls; extern crate kvm_bindings; extern crate libc; use std::{ffi::CString, fs::File, io::{Read, Write}, ptr::null_mut, slice, thread::JoinHandle, time::Duration}; use kvm_bindings::{kvm_userspace_memory_region, KVM_MEM_LOG_DIRTY_PAGES}; use kvm_ioctls::{Kvm, VcpuExit, VcpuFd, VmFd}; use std::thread...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/password_generator/src/bin/password_generator.rs
手撸系列/password_generator/src/bin/password_generator.rs
//!实现一个通用的密码生成器 use anyhow::{bail, Error, Ok}; use base64::prelude::*; use clap::Parser; use rand::prelude::*; // 计算seed种子的hash值 fn seed_hash(seed: &str) -> usize { let mut hash: usize = 0; for (i, c) in seed.chars().enumerate() { hash += (i + 1) * (c as usize); } (hash % 31).pow(3) } // 密码子,...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/thread_pool/src/lib.rs
手撸系列/thread_pool/src/lib.rs
use std::{ sync::{ mpsc::{self, Receiver, Sender}, Arc, Mutex, }, thread::{self, JoinHandle}, time::SystemTime, }; type Job = Box<dyn FnOnce() + Send>; pub struct Threadpool { workers: Vec<Worker>, sender: Option<Sender<Job>>, } struct Worker { id: usize, handle: Option...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/thread_pool/src/main.rs
手撸系列/thread_pool/src/main.rs
fn main() { println!("Hello, world!"); }
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/mini_vm/src/bin/mini_vm.rs
手撸系列/mini_vm/src/bin/mini_vm.rs
//! mini virtual machine 迷你虚拟机 #[derive(Debug)] struct Instruct { // 指令 code: IP, // 执行指令的条件 cond: Condition, // 指令的参数指针 p1: i32, p2: i32, } // 指令枚举 #[derive(Debug)] enum IP { IADD, // 加法 ISUB, // 减法 ICMP, // 判断 IJMP, // 跳转 IMOV, // 赋值 ISTIP, // 保存ip ILDIP, /...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/main.rs
手撸系列/nom-practice/src/main.rs
//! 时序数据库协议解析 use nom::{ branch::alt, bytes::complete::{tag, tag_no_case, take_while1}, character::complete::{digit1, one_of}, combinator::{map, map_res, opt, recognize, value}, error::ErrorKind, multi::{many1, separated_list1}, sequence::{delimited, pair, preceded, separated_pair, terminate...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/data.rs
手撸系列/nom-practice/src/data.rs
pub static DATA: &'static str = r#"onview,name=event_0,fleet=South,driver=Trish,model=H_2,device_version=v2_3 latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0,fuel_consumption=25,load_capacity=0,fuel_capacity=0,nominal_fuel_consumption=0 1640995200000000000 onview,name=event_1,fleet=Sout...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
true
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/bin/hello_parser_generator.rs
手撸系列/nom-practice/src/bin/hello_parser_generator.rs
fn main() { let msg = "hello,rust"; let mut parse_generator = hello_parser_generator("hello"); let hello1 = parse_generator(msg); println!("parse result:{:?}", hello1); } /// hello解析器生成器 /// 返回参数:剩余字符串,解析出来的字符串 fn hello_parser_generator<'a>( tag: &'a str, ) -> impl FnMut(&'a str) -> Result<(&'a str...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/bin/parser_combinor.rs
手撸系列/nom-practice/src/bin/parser_combinor.rs
fn main() { let msg = "hello,rust"; let result = parser_combinor(msg, hello_parser, comma_parser, rust_parser); println!("result:{:?}", result); } /// hello解析器 /// 返回参数:剩余字符串,解析出来的字符串 fn hello_parser(msg: &str) -> Result<(&str, &str), &str> { if !msg.is_empty() && msg.starts_with("hello") { Ok(...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/bin/hello_parser.rs
手撸系列/nom-practice/src/bin/hello_parser.rs
fn main() { let msg = "hello,rust"; let hello = hello_parser(msg); println!("parse result:{:?}", hello); } /// hello解析器 /// 返回参数:剩余字符串,解析出来的字符串 fn hello_parser(msg: &str) -> Result<(&str, &str), &str> { if !msg.is_empty() && msg.starts_with("hello") { Ok((&msg["hello".len()..], "hello")) } ...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
reganzm/hug_rust
https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/手撸系列/nom-practice/src/bin/nom_functions.rs
手撸系列/nom-practice/src/bin/nom_functions.rs
use nom::{ branch::alt, bytes::complete::tag, combinator::{consumed, map, peek, recognize}, multi::{many0, many1}, sequence::{delimited, pair, preceded, separated_pair, terminated, tuple}, IResult, }; fn main() { let msg = "hello,rust"; let msg1 = "hellohello,rust"; println!("tag:{:?...
rust
Apache-2.0
6a2801769c8b062477c2e6273af0218e0e21e75d
2026-01-04T20:24:35.074754Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/config.rs
src/config.rs
//! Configuration structs for the developer console. use bevy::log::Level; use bevy::prelude::*; use bevy_egui::egui::{Color32, FontId, TextFormat}; /// The configuration of the developer console. #[derive(Resource, Reflect, Debug)] pub struct ConsoleConfig { /// The colors used in the developer console. pub ...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/prelude.rs
src/prelude.rs
//! `use bevy_dev_console::prelude::*` to quickly import the required plugins for [`bevy_dev_console`](crate). pub use crate::config::ConsoleConfig; pub use crate::logging::custom_log_layer; pub use crate::DevConsolePlugin;
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/lib.rs
src/lib.rs
#![doc = include_str!("../README.md")] use bevy::prelude::*; use bevy_egui::EguiPlugin; use command::CommandHints; use config::ConsoleConfig; use ui::ConsoleUiState; #[cfg(feature = "builtin-parser")] pub mod builtin_parser; pub mod command; pub mod config; pub mod logging; pub mod prelude; pub mod ui; /// Adds a De...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/command.rs
src/command.rs
//! Command execution functionality. use std::borrow::Cow; use std::ops::Range; use bevy::ecs::world::Command; use bevy::prelude::*; /// The command parser currently being used by the dev console. #[derive(Resource)] pub struct DefaultCommandParser(pub Box<dyn CommandParser>); impl DefaultCommandParser { /// Sh...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/builtin_parser.rs
src/builtin_parser.rs
//! [`bevy_dev_console`](crate)'s built-in command parser. //! //! Currently the built-in command parser is in very early development. //! It's purpose is to provide a simple, yet powerful method of modifying //! the game world via commands. use bevy::prelude::*; use logos::Span; use crate::builtin_parser::runner::Ex...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/ui.rs
src/ui.rs
//! The module that handles the user interface of the console. //! //! Made with [`bevy_egui`]. use bevy::prelude::*; use bevy_egui::egui::text::LayoutJob; use bevy_egui::egui::{Stroke, TextFormat}; use bevy_egui::*; use chrono::prelude::*; use web_time::SystemTime; use crate::command::{CommandHints, ExecuteCommand};...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/logging.rs
src/logging.rs
//! Custom [LogPlugin](bevy::log::LogPlugin) functionality. use bevy::log::{BoxedLayer, Level}; use bevy::prelude::*; use bevy::utils::tracing::Subscriber; use std::sync::mpsc; use tracing_subscriber::field::Visit; use tracing_subscriber::Layer; use web_time::SystemTime; /// A function that implements the log reading...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false
doonv/bevy_dev_console
https://github.com/doonv/bevy_dev_console/blob/98355f16b7d83ad0db983f2204426079780aa2ae/src/builtin_parser/lexer.rs
src/builtin_parser/lexer.rs
//! Generates a stream of tokens from a string. use logos::{Lexer, Logos, Span}; #[derive(Debug, Clone, Default, PartialEq)] pub struct FailedToLexCharacter; #[derive(Logos, Debug, Clone, PartialEq)] #[logos(skip r"[ \t\n\f]+", error = FailedToLexCharacter)] pub enum Token { #[token("(")] LeftParen, #[to...
rust
Apache-2.0
98355f16b7d83ad0db983f2204426079780aa2ae
2026-01-04T20:24:35.637367Z
false