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/rust基础/智能指针/src/bin/weak_pointer.rs | rust基础/智能指针/src/bin/weak_pointer.rs | use std::{
cell::RefCell,
rc::{Rc, Weak},
};
fn main() {
let car = Rc::new(Car {
name: "国货之光-仰望U8".to_string(),
wheels: RefCell::new(vec![]),
});
let wheel1 = Rc::new(Wheel {
id: 1,
car: car.clone(),
});
let wheel2 = Rc::new(Wheel {
id: 2,
ca... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/智能指针/src/bin/cow.rs | rust基础/智能指针/src/bin/cow.rs | use std::borrow::Cow;
fn main() {
fn delete_space<'a>(src: &'a str) -> Cow<'a, str> {
if src.contains(' ') {
let mut dest = String::with_capacity(src.len());
for c in src.chars() {
if ' ' != c {
dest.push(c);
}
}
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/智能指针/src/bin/cell_and_refcell.rs | rust基础/智能指针/src/bin/cell_and_refcell.rs | use std::{
borrow::Borrow,
cell::{Cell, RefCell},
collections::HashMap,
ops::{Add, Shl},
rc::Rc,
};
fn main() {
// Cell
let regan = Student {
age: 30,
id: Cell::new(10000000888),
};
println!("regan:{:?}", regan);
// 编译报错,age不可变
//regan.age = 28;
// regan.i... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/option_and_result.rs | rust基础/异常处理/src/bin/option_and_result.rs | fn main() {
// or 和 and方法
// Option
let s1 = Some("123");
let s2 = Some("234");
let s3: Option<&str> = None;
assert_eq!(s1.or(s2), s1);
assert_eq!(s1.and(s2), s2);
assert_eq!(s1.and(s3), s3);
assert_eq!(s1.or(s3), s1);
// Result
let ok1: Result<&str, &str> = Ok("ok1");
le... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/self_define_error_type.rs | rust基础/异常处理/src/bin/self_define_error_type.rs | use std::{
fmt::Debug,
fs::File,
io::{self, ErrorKind},
};
fn main() {
fn get_a_err1() -> Result<(), MyError> {
Err(MyError {
code: 404,
message: String::from("找不到页面"),
})
}
fn get_a_err2() -> Result<(), MyError> {
Err(MyError {
code: ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/result.rs | rust基础/异常处理/src/bin/result.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/rust基础/异常处理/src/bin/use_anyhow.rs | rust基础/异常处理/src/bin/use_anyhow.rs | use std::{fs::File, io::Read};
use anyhow::{ Error, Ok, Result};
fn main() -> Result<()> {
let result = test_fn();
println!("{:?}",result);
Ok(())
}
fn test_fn() -> Result<String,Error> {
let data_path = std::env::var("DATA_PATH")?;
let mut file = File::open(data_path)?;
let mut buf = String::n... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/use_thiserror.rs | rust基础/异常处理/src/bin/use_thiserror.rs | use std::{fs::File, io::Read};
#[warn(dead_code)]
fn main() {
let r = test_fn();
match r {
Ok(s) => println!("{s}"),
Err(myerror) => println!("error:{:?}", myerror),
}
}
fn test_fn() -> Result<String, MyError> {
let data_path = std::env::var("DATA_PATH")?;
let mut file = File::open... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/open_file_with_result.rs | rust基础/异常处理/src/bin/open_file_with_result.rs | use std::{
fs::File,
io::{Error, ErrorKind, Read, Write},
};
fn main() -> Result<(), Error> {
let file = File::open("./src/bin/result.txt");
// file是Result<File,Error>类型。现在做模式匹配
let mut file = match file {
Ok(file) => file,
Err(err) => match err.kind() {
ErrorKind::NotFo... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/异常处理/src/bin/open_file_v2.rs | rust基础/异常处理/src/bin/open_file_v2.rs | pub mod use_thiserror;
use std::{
fs::File,
io::{Error, ErrorKind, Read, Write},
};
fn main() -> Result<(), Error> {
let mut file = File::open("./src/bin/result.txt").expect("打开文件失败");
// 写入数据
let _ = file.write_all("2024加油!".as_bytes());
// 读取数据
let mut file = File::open("./src/bin/result... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/hello/src/main.rs | rust基础/hello/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/rust基础/pin_and_unpin/src/bin/pin_rabbit_heap.rs | rust基础/pin_and_unpin/src/bin/pin_rabbit_heap.rs | use std::marker::PhantomPinned;
use std::pin::Pin;
/// 将值Pin在栈上
// 创建一个自引用类型的兔子结构体
#[derive(Debug)]
struct Rabbit {
name: String,
p: *const String,
_marker: PhantomPinned,
}
impl Rabbit {
fn new(txt: &str) -> Pin<Box<Self>> {
let rabbit = Rabbit {
name: String::from(txt),
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/async_concurrent_mult_thread_web_server.rs | rust基础/pin_and_unpin/src/bin/async_concurrent_mult_thread_web_server.rs | use std::{fs,time::Duration};
use async_std::net::TcpListener;
use async_std::net::TcpStream;
use async_std::task;
use futures::stream::StreamExt;
use async_std::prelude::*;
use async_std::task::spawn;
#[async_std::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").await.unwrap();
list... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/async_std_web_server.rs | rust基础/pin_and_unpin/src/bin/async_std_web_server.rs | use std::{fs, io::{Read, Write}, net::{TcpListener, TcpStream}, time::Duration};
use async_std::task;
#[async_std::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
// 警告,这里无法并发
handl... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/pin_rabbit_stack.rs | rust基础/pin_and_unpin/src/bin/pin_rabbit_stack.rs | use std::marker::PhantomPinned;
use std::pin::Pin;
/// 将值Pin在栈上
// 创建一个自引用类型的兔子结构体
#[derive(Debug)]
struct Rabbit {
name: String,
p: *const String,
_marker: PhantomPinned,
}
impl Rabbit {
fn new(txt: &str) -> Self {
Rabbit {
name: String::from(txt),
p: std::ptr::null(),
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/async_concurrent_single_thread_web_server.rs | rust基础/pin_and_unpin/src/bin/async_concurrent_single_thread_web_server.rs | use std::{fs,time::Duration};
use async_std::net::TcpListener;
use async_std::net::TcpStream;
use async_std::task;
use futures::stream::StreamExt;
use async_std::prelude::*;
#[async_std::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").await.unwrap();
listener
.incoming()
.f... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/self_ref.rs | rust基础/pin_and_unpin/src/bin/self_ref.rs | // 创建一个自引用类型的兔子结构体
#[derive(Debug)]
struct Rabbit {
name: String,
p: *const String,
}
impl Rabbit {
fn new(txt: &str) -> Self {
Rabbit {
name: String::from(txt),
p: std::ptr::null(),
}
}
fn init(&mut self) {
let self_ref: *const String = &self.name;
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/pin_and_unpin/src/bin/single_thread_web_server.rs | rust基础/pin_and_unpin/src/bin/single_thread_web_server.rs | use std::{fs, io::{Read, Write}, net::{TcpListener, TcpStream}};
// 单线程版本的web服务
fn main() {
// 监听本地端口 12345
let listener = TcpListener::bind("127.0.0.1:12345").unwrap();
// 阻塞等待请求进入
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/泛型/src/main.rs | rust基础/泛型/src/main.rs | use std::{
cmp::max,
fmt::{Debug, Display},
ops::{Add, Mul},
};
fn main() {
// 编译阶段T被替换为i32类型
let s: i32 = sum(120, 20);
println!("s:{s}");
// 编译阶段T被替换为f32类型
let s: f32 = sum(120_f32, 20_f32);
println!("s:{s}");
let arr = [1, 2, 3, 4, 5, 6];
let lar = largest(&arr);
pri... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/rust_编程规范/src/main.rs | rust基础/rust_编程规范/src/main.rs | //! 熟悉Rust中的各种约定用法
//! 熟悉Rust中的基本概念
#[warn(unused_variables)]
#[warn(non_snake_case)]
fn main() {
let result1: i32 = sum(10, 20);
assert_eq!(30, result1);
// 函数默认返回类型为单元类型
let result2: () = test_function();
// 使用println!宏来打印输出,使用{result1}作为占位符,用于输出result1;使用{:?}作为占位符,输出result2。
// 因为单元类型没有实现Dis... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/集合了集合/src/bin/hashmap.rs | rust基础/集合了集合/src/bin/hashmap.rs | use std::collections::HashMap;
fn main() {
// 使用关联函数new创建
let mut map = HashMap::new();
map.insert("lucky".to_string(), 95);
map.insert("lily".to_string(), 100);
println!("map:{:?}", map);
// 使用迭代器+collect创建
let props = vec![("lucky".to_string(), 95), ("lily".to_string(), 100)];
let mu... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/集合了集合/src/bin/vec.rs | rust基础/集合了集合/src/bin/vec.rs | fn main() {
// 动态数组
// 使用vec!宏创建
let v = vec![2, 3, 4, 5, 6, 76];
println!("v:{:?}", v);
// 使用关联函数new创建
let mut vv: Vec<i32> = Vec::new();
// 使用push添加元素
vv.push(1);
vv.push(2);
vv.push(3);
println!("vv:{:?}", vv);
// 从动态数组中获取元素
println!("第6个元素:{:?}", v.get(6));
/... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生命周期/src/bin/lifecycle.rs | rust基础/生命周期/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/rust基础/客户端服务端聊天chat/server/src/main.rs | rust基础/客户端服务端聊天chat/server/src/main.rs | use std::io::{ErrorKind, Read, Write};
use std::net::TcpListener;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
// 监听地址
const LOCAL: &str = "127.0.0.1:8888";
// 消息大小
const MSG_SIZE: usize = 32;
fn main() {
// 建立监听
let server = TcpListener::bind(LOCAL).expect("绑定失败");
// 设置server的accept方法为... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/客户端服务端聊天chat/client/src/main.rs | rust基础/客户端服务端聊天chat/client/src/main.rs | use std::io::{self, ErrorKind, Read, Write};
use std::net::TcpStream;
use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
// 服务端地址
const LOCAL: &str = "127.0.0.1:8888";
// 消息长度
const MSG_SIZE: usize = 32;
fn main() {
// 和客户端建立连接
let mut client = TcpStream::connect(LOCAL).expec... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/unsafe的超能力/src/bin/raw_pointer.rs | rust基础/unsafe的超能力/src/bin/raw_pointer.rs | use std::{slice::from_raw_parts, str::from_utf8_unchecked};
fn main() {
create_raw_point();
//random_mem_addr_as_raw_pointer();
let hi = "Hi 2024";
// 获取“Hi 2024”的内存地址
let hi_addr = hi.as_ptr() as usize;
// 字符串长度
let len = hi.len();
// 从指定内存地址处读取指定长度的数据
let data = unsafe { from_utf... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/函数式编程/src/main.rs | rust基础/函数式编程/src/main.rs | use core::num;
fn main() {
// 定义闭包
// 判断是否为奇数
let is_odd_number = |num: u32| num % 2 != 0;
let a = 10;
let b = 11;
// 像普通函数一样使用
println!(
"a is odd number:{} b is odd number:{}",
is_odd_number(a),
is_odd_number(b)
);
// 闭包体捕获外部变量
let base = 11;
let ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/复合数据类型/src/main.rs | rust基础/复合数据类型/src/main.rs | use std::any::Any;
fn main() {
// 元组
// 定义一个元组
let tuple1 = ();
println!("tuple1:{:?}", tuple1);
// 定义一个拥有相同数据类型的元组
let tuple2 = (2, 4, 6, 8, 10);
println!("tuple2:{:?}", tuple2);
// 定义一个拥有不同数据类型的元组
let tuple3 = (1, 3.14, 9877_u32, 87658.9878_f64, true, '😄');
println!("tuple3:{... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/宏/src/main.rs | rust基础/宏/src/main.rs | macro_rules! sum_all {
($($arg:expr),*) => {
{
let mut sum = 0;
$(sum +=$arg;)*
sum
}
};
}
fn main() {
let result = sum_all!(1, 2, 3, 4, 5);
println!("The sum is: {}", result); // 输出: The sum is: 15
let result = sum_all!(1, 2, 3, 4, 5, 6, 7, 8, 9)... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/特征/src/main.rs | rust基础/特征/src/main.rs | use std::{
fmt::{Debug, Display},
ops::Add,
};
fn main() {
// 定义特征
// 为类型实现特征
let tiger = Tiger::new(3, "OldSix");
tiger.behavior();
// 结构体方法传入特征
tiger.show(&tiger);
// 结构体方法返回特征
let action = tiger.do_action();
action.eat();
// 打破孤儿规则
struct MyString(String);
im... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/点操作符的魔力/src/bin/magic_of_point.rs | rust基础/点操作符的魔力/src/bin/magic_of_point.rs | use std::rc::Rc;
fn main() {
let array: Rc<Box<[i32; 10]>> = Rc::new(Box::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));
let first_element = array.get(0);
println!("first element:{:?}", first_element);
let mut rc = Rc::new(10);
let rc1 = &rc;
let mut rc1 = &mut rc;
let array1 = array.clone();
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/once_init.rs | rust基础/并发和并行/src/bin/once_init.rs | use std::{sync::Once, thread, time::Duration};
fn main() {
static mut VERSION: &str = "1.2.3";
static INIT: Once = Once::new();
let handle1 = thread::spawn(move || {
//thread::sleep(Duration::from_millis(10));
INIT.call_once(|| unsafe {
VERSION = "1.2.5";
});
});
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/move_ownership.rs | rust基础/并发和并行/src/bin/move_ownership.rs | use std::{thread, time::Duration};
fn main() {
let hello = String::from("Hello Rust");
let handle = thread::spawn(move || {
// 线程睡眠1000毫秒
thread::sleep(Duration::from_millis(1000));
// 打印出当前线程id
println!("{} , thread_id:{:?}", hello, thread::current().id());
});
handle.jo... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/condition_var_and_mutex.rs | rust基础/并发和并行/src/bin/condition_var_and_mutex.rs | use std::borrow::Borrow;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
println!("main 锁定的值为:{:?}", pair.0.lock().unwrap());
thread::spawn(move || {
let (lock, cvar)... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/thread_barrier.rs | rust基础/并发和并行/src/bin/thread_barrier.rs | use std::{
sync::{Arc, Barrier},
thread::{self, JoinHandle},
};
fn main() {
let cap: usize = 11;
let mut handles: Vec<JoinHandle<()>> = Vec::with_capacity(cap);
// 通过Arc在堆上创建多线程安全且共享所有权的Barrier,屏障初始为11个线程
let barrier: Arc<Barrier> = Arc::new(Barrier::new(cap));
for i in 0..=10 {
// ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/wait_child_thread.rs | rust基础/并发和并行/src/bin/wait_child_thread.rs | use std::{thread, time::Duration};
fn main() {
let handle = thread::spawn(|| {
// 线程睡眠1000毫秒
thread::sleep(Duration::from_millis(1000));
// 打印出当前线程id
println!(
"Hello Rust from thread , thread_id:{:?}",
thread::current().id()
);
});
// 主线程打印出当... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/thread_local.rs | rust基础/并发和并行/src/bin/thread_local.rs | use std::cell::Cell;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use thread_local::ThreadLocal;
fn main() {
// 使用标准库中的thread_local!宏创建线程局部变量
// 使用Cell提供内部可变性
thread_local! { static INIT:Cell<i32> = Cell::new(100)};
INIT.with(|i| {
println!("main thread i={}", i.get());
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/并发和并行/src/bin/first_thread.rs | rust基础/并发和并行/src/bin/first_thread.rs | use std::{thread, time::Duration};
fn main() {
thread::spawn(|| {
// 线程睡眠1000毫秒
thread::sleep(Duration::from_millis(1000));
// 打印出当前线程id
println!(
"Hello Rust from thread , thread_id:{:?}",
thread::current().id()
);
});
// 主线程打印出当前线程id
pri... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/auto_test/src/lib.rs | rust基础/auto_test/src/lib.rs | /// 判断一个数是否为奇数
fn odd_number(a: usize) -> bool {
let mut result = true;
if a % 2 == 0 {
result = false
};
result
}
/// 判断一个数是否是偶数
fn even_number(a: usize) -> bool {
!odd_number(a)
}
/// 判断一个数是否能被5整除
/// `````
/// let a = 125_usize;
/// let r = auto_test::devide_by_5(a);
/// assert_eq!(r,tru... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/auto_test/src/main.rs | rust基础/auto_test/src/main.rs | fn main(){
let a = 3;
let b = 1 + 3;
debug_assert_eq!(a, b, "我们在测试两个数之和{} + {},这是额外的错误信息", a, b);
} | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/auto_test/tests/integration_test1.rs | rust基础/auto_test/tests/integration_test1.rs | use auto_test;
#[test]
fn test_devide_by_5() {
assert_eq!(auto_test::devide_by_5(555), true);
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/auto_test/benches/benchmark_test1.rs | rust基础/auto_test/benches/benchmark_test1.rs | use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn pow_5(num:i32)->i32{
num * num * num * num * num
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("pow_5", |b| b.iter(|| pow_5(black_box(100))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/async_programming/src/lib.rs | rust基础/async_programming/src/lib.rs | use std::{
future::Future,
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll, Waker},
thread,
time::Duration,
};
pub struct TimerFuture {
shared_state: Arc<Mutex<SharedState>>,
}
/// 在Future和等待的线程间共享状态
struct SharedState {
completed: bool,
waker: Option<Waker>,
}
impl Future for ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/async_programming/src/main.rs | rust基础/async_programming/src/main.rs | use {
// 引入之前实现的定时器模块
async_programming::TimerFuture,
futures::{
future::{BoxFuture, FutureExt},
task::{waker_ref, ArcWake},
},
std::{
future::Future,
sync::mpsc::{sync_channel, Receiver, SyncSender},
sync::{Arc, Mutex},
task::Context,
time::Du... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/async_programming/src/bin/async_join.rs | rust基础/async_programming/src/bin/async_join.rs | use futures::{executor::block_on, FutureExt};
/// join并发的执行多个Future
fn main() {
// 使用异步的方式计算(1+1)*(2+2)
async fn one_add_one() -> i32 {
1 + 1
}
async fn two_add_two() -> i32 {
2 + 2
}
async fn part_mult_part() -> i32 {
let f1 = one_add_one();
let f2 = two_add_two... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/async_programming/src/bin/async_hello_world.rs | rust基础/async_programming/src/bin/async_hello_world.rs | use futures::executor::block_on;
/// 异步编程的Hello world
fn main() {
// 使用async fn创建异步函数
async fn async_hi() {
println!("hello world from async");
}
// 调用async_hi异步函数
let result = async_hi();
block_on(result);
async fn async_hello() {
println!("hello");
async_world().a... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/src/main.rs | rust基础/生产级别workspace/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/rust基础/生产级别workspace/crates/services/printer/src/testx.rs | rust基础/生产级别workspace/crates/services/printer/src/testx.rs | pub fn testx(){
println!("testx");
} | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/services/printer/src/main.rs | rust基础/生产级别workspace/crates/services/printer/src/main.rs | // 在包里面引用其它的包
use lib_core::core1::core1_test;
use lib_core::core2::core_test2;
use lib_core::core3::fn1::fn1;
use lib_core::core3::fn2::fn2;
use lib_utils::util1::util1;
use lib_utils::util2::util2;
// 使用as命名别名解决命名冲突
use lib_utils::utils3::fn1::fn1 as util_fn1;
use lib_utils::utils3::fn2::fn2 as util_fn2;
mod testx;
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/lib.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/lib.rs | // 对外暴露模块
pub mod util1;
pub mod util2;
pub mod utils3;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/util1.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/util1.rs | pub fn util1() {
println!("lib-utils util1");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/util2.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/util2.rs | pub fn util2() {
println!("lib-utils util2");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/fn2.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/fn2.rs | pub fn fn2() {
println!("lib-utils util3 fn2");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/fn1.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/fn1.rs | pub fn fn1() {
println!("lib-utils util3 fn1");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/mod.rs | rust基础/生产级别workspace/crates/libs/lib-utils/src/utils3/mod.rs | pub mod fn1;
pub mod fn2;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/core2.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/core2.rs | pub fn core_test2() {
println!("lib-core core2 core_test2")
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/lib.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/lib.rs | // 对外暴露模块
pub mod core1;
pub mod core2;
pub mod core3;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/core1.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/core1.rs | // pub关键字将模块暴露给外面使用
pub fn core1_test() {
println!("lib-core core1 core1_test")
}
// 这个方法在该模块外无法访问到
fn core1_test1() {
println!("lib-core core1 core1_test1")
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/core3/fn2.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/core3/fn2.rs | pub fn fn2() {
println!("lib-core core3 fn2");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/core3/fn1.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/core3/fn1.rs | pub fn fn1() {
println!("lib-core core3 fn1");
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/生产级别workspace/crates/libs/lib-core/src/core3/mod.rs | rust基础/生产级别workspace/crates/libs/lib-core/src/core3/mod.rs | // 对外暴露fn1和fn2
pub mod fn1;
pub mod fn2;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/类型别名/src/main.rs | rust基础/类型别名/src/main.rs |
fn main() {
type Hour = u8;
type Minute = u8;
let a: u8 = 23;
let hour: Hour = 23;
println!("a == hour : {}", a == hour);
let b: u8 = 59;
let minute: Minute = 59;
println!("b == minute : {}", b == minute);
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/认识变量常量和标量/src/main.rs | rust基础/认识变量常量和标量/src/main.rs | // 包级别静态变量,不允许遮盖
static mut AGE: i32 = 30;
// 遮盖报错
//static mut AGE:i32 = 30;
fn main() {
// 不会被使用到的变量,使用_开头来定义
let _age: i32 = 33;
// 编译报错,不可变变量不能改变值
//age = 31;
let mut height: i32 = 175;
// 可变变量可以重新绑定值,重新绑定后位于栈中的175将被自动释放掉
height -= 5;
println!("height:{height}");
// 可以使用let覆盖之前定义... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/rc_not_safe.rs | rust基础/线程同步和线程安全/src/bin/rc_not_safe.rs | use std::{rc::Rc, sync::Arc, thread};
/// 在线程中使用Rc智能指针
fn main() {
let _t = Rc::new(100);
Arc::new(10);
let handler = thread::spawn(move || {
// 下面打印会报错
// println!("t:{:?}", t);
});
handler.join().unwrap();
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mpsc_multi.rs | rust基础/线程同步和线程安全/src/bin/mpsc_multi.rs | use std::{sync::mpsc, thread};
fn main() {
// 创建一个通道,返回元组(发送者,接收者)
let (sender, receiver) = mpsc::channel();
// 克隆一个生产者
let sender1 = sender.clone();
// 生产者1
thread::spawn(move || {
let thread_id = thread::current().id();
let msg = format!("加油2024 来自线程:{:?}", thread_id);
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/atomic_and_mutex.rs | rust基础/线程同步和线程安全/src/bin/atomic_and_mutex.rs | use std::ops::Sub;
use std::{
sync::{
atomic::{AtomicU32, Ordering},
Arc, Mutex,
},
thread::{self, JoinHandle},
time::Instant,
};
// 使用Atomic和Mutex做比较
fn main() {
// 原子类型作为全局变量
const N_TIMES: u32 = 1000000;
const CAP: usize = 100;
static RESULT: AtomicU32 = AtomicU32::ne... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/for_mpsc.rs | rust基础/线程同步和线程安全/src/bin/for_mpsc.rs | use std::{sync::mpsc, thread, time::Duration};
fn main() {
// 创建一个通道,返回元组(发送者,接收者)
let (sender, receiver) = mpsc::channel();
// 创建子线程向主线程发送消息
thread::spawn(move || {
for i in 1..=10 {
// 使用move将sender的所有权移动到子线程中
// send方法返回Result,因为能发送失败
// 这里直接unwrap取出Ok
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mutex_in_multi_thread.rs | rust基础/线程同步和线程安全/src/bin/mutex_in_multi_thread.rs | use std::{
sync::{Arc, Mutex},
thread,
};
/// 多线程中使用Mutex
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
// 新建50个线程,每个线程对counter做加1操作
for _ in 1..=50 {
// 使用共享所有权且线程安全的智能指针Arc,如果使用Rc会报错
let counter = counter.clone();
let handle = thread::spa... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mutex1.rs | rust基础/线程同步和线程安全/src/bin/mutex1.rs | use std::sync::Mutex;
fn main() {
// 使用Mutex创建互斥锁实例
let m = Mutex::new(10);
{
// lock方法获取MutexGuard
// MutexGuard实现了Deref特征和Drop特征
let mut lock: std::sync::MutexGuard<i32> = m.lock().unwrap();
// 通过*解引用获取值,并修改值
*lock = *lock - 1;
} // <--- num离开作用域时,自动调用drop释放锁
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/memory_order.rs | rust基础/线程同步和线程安全/src/bin/memory_order.rs | use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::{hint, thread};
fn main() {
let spinlock = Arc::new(AtomicUsize::new(1));
let spinlock_clone = Arc::clone(&spinlock);
let thread = thread::spawn(move || {
spinlock_clone.store(100, Ordering::Release); // 内存屏障1↑
});
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mutex_and_condvar.rs | rust基础/线程同步和线程安全/src/bin/mutex_and_condvar.rs | use std::{
sync::{Arc, Condvar, Mutex},
thread,
time::Duration,
};
/// mutex和condvar一起控制线程同步
fn main() {
// 在线程之间共享所有权
let lock = Arc::new(Mutex::new(false));
let con = Arc::new(Condvar::new());
let lock_1 = lock.clone();
let con_1 = con.clone();
let handle = thread::spawn(move ||... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/dead_lock_by_mutex.rs | rust基础/线程同步和线程安全/src/bin/dead_lock_by_mutex.rs | use std::sync::Mutex;
fn main() {
// 使用Mutex创建互斥锁实例
let m = Mutex::new(10);
{
// lock方法获取MutexGuard
// MutexGuard实现了Deref特征和Drop特征
let mut lock: std::sync::MutexGuard<i32> = m.lock().unwrap();
// 通过*解引用获取值,并修改值
*lock = *lock - 1;
//drop(lock);
// 再次获取... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/send_multi_type_data.rs | rust基础/线程同步和线程安全/src/bin/send_multi_type_data.rs | use std::{sync::mpsc, thread};
fn main() {
// 创建枚举
#[derive(Debug)]
enum Animal {
Dog(String, u8),
Cat(String, u8),
Fish(String, u8, f32),
}
let (sender, receiver) = mpsc::channel();
thread::spawn(move || {
let dog = Animal::Dog(String::from("憨憨"), 3);
let... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mpsc_single.rs | rust基础/线程同步和线程安全/src/bin/mpsc_single.rs | use std::{sync::mpsc, thread};
fn main() {
// 创建一个通道,返回元组(发送者,接收者)
let (sender, receiver) = mpsc::channel();
// 创建子线程向主线程发送消息
thread::spawn(move || {
// 使用move将sender的所有权移动到子线程中
// send方法返回Result,因为能发送失败
// 这里直接unwrap取出Ok
let result = sender.send("加油2024");
if le... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/impl_send_for_pointer.rs | rust基础/线程同步和线程安全/src/bin/impl_send_for_pointer.rs | use std::thread;
/// 为裸指针实现Send特征
fn main() {
let num = 100;
let p = num as *mut i32;
let p = SandBox(p);
let handler = thread::spawn(move || {
println!("{:?}", p);
});
// 下面打印会报错,因为p的所有权已经被转移到子线程中去了
// println!("{:?}",p);
handler.join().unwrap();
}
#[derive(Debug)]
struct Sand... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/tokio_semaphore.rs | rust基础/线程同步和线程安全/src/bin/tokio_semaphore.rs | use std::{sync::Arc, thread, time::Duration};
use tokio::sync::Semaphore;
#[tokio::main]
async fn main() {
// 初始化信号量为3
let semaphore: Arc<Semaphore> = Arc::new(Semaphore::new(3));
let mut join_handles = Vec::new();
// 获取一个信号量许可,信号量减1
let permit = semaphore.clone().acquire_owned().await.unwrap();
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/rw_lock.rs | rust基础/线程同步和线程安全/src/bin/rw_lock.rs | use std::sync::RwLock;
/// 读写锁
fn main() {
// 实例化读写锁
let lock = RwLock::new(100);
// 同一时间允许多个读
{
let num1 = lock.read().unwrap();
let num2 = lock.read().unwrap();
assert_eq!(*num1, 100);
assert_eq!(*num2, 100)
} //<-------------读锁在此离开作用域,释放读锁
// 同一时间只运行一个写
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/mpsc_try_recv.rs | rust基础/线程同步和线程安全/src/bin/mpsc_try_recv.rs | use std::{sync::mpsc, thread, time::Duration};
fn main() {
// 创建一个通道,返回元组(发送者,接收者)
let (sender, receiver) = mpsc::channel();
// 创建子线程向主线程发送消息
thread::spawn(move || {
// 使用move将sender的所有权移动到子线程中
// send方法返回Result,因为能发送失败
// 这里直接unwrap取出Ok
let msg = String::from("加油2024");
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/rust基础/线程同步和线程安全/src/bin/impl_send_sync_for_pointer.rs | rust基础/线程同步和线程安全/src/bin/impl_send_sync_for_pointer.rs | use std::{sync::Arc, thread};
/// 为裸指针实现send和sync特征
fn main() {
let num = 123;
let sand_box = Arc::new(SandBox(num as *mut i32));
let sand_box_clone = sand_box.clone();
let handle = thread::spawn(move || {
println!("sand box value:{:?}", sand_box_clone);
});
handle.join().unwrap();
p... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/self_in_cpu/src/main.rs | 提升篇/self_in_cpu/src/main.rs | //! cpu眼里的self
pub fn get(pointer: *const A) -> *const A {
pointer
}
pub struct A;
impl A {
pub fn get(&self) -> *const A {
self as *const A
}
}
pub fn main() {
let b = A.get();
let d = get(b);
println!("b:{:p} d:{:p}", b, d);
}
// example::get::h64f063fea484ae4e:
// mov r... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/routers.rs | 提升篇/actix_web_service/src/routers.rs | use crate::handlers::{course::*, general::health_check_handler};
use actix_web::web::{self};
pub fn general_routes(cfg: &mut web::ServiceConfig) {
cfg.route("/health", web::get().to(health_check_handler));
}
pub fn course_routes(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/courses")
... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/state.rs | 提升篇/actix_web_service/src/state.rs | use std::sync::Mutex;
use sqlx::PgPool;
pub struct AppState {
pub health_check_response: String,
pub visit_count: Mutex<i32>,
pub db: PgPool,
}
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/error.rs | 提升篇/actix_web_service/src/error.rs | use actix_web::{error, http::StatusCode, HttpResponse};
use serde::Serialize;
use sqlx::error::Error as SQLError;
use std::fmt;
#[derive(Debug, Serialize)]
pub enum MyError {
DBError(String),
ActixError(String),
NotFound(String),
}
#[derive(Debug, Serialize)]
pub struct MyErrorResponse {
error_message... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/db_access/course.rs | 提升篇/actix_web_service/src/db_access/course.rs | use crate::error::MyError;
use crate::models::course::{Course, CreateCourse, UpdateCourse};
use sqlx::postgres::PgPool;
pub async fn get_courses_for_teacher_db(
pool: &PgPool,
teacher_id: i32,
) -> Result<Vec<Course>, MyError> {
let rows: Vec<Course> = sqlx::query_as!(
Course,
r#"SELECT * F... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/db_access/lib.rs | 提升篇/actix_web_service/src/db_access/lib.rs | pub mod course;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/models/course.rs | 提升篇/actix_web_service/src/models/course.rs | use actix_web::web;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use crate::error::MyError;
use std::convert::TryFrom;
#[derive(Serialize, Debug, Clone, sqlx::FromRow)]
pub struct Course {
pub id: i32,
pub teacher_id: i32,
pub name: String,
pub time: Option<NaiveDateTime>,
pub d... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/models/lib.rs | 提升篇/actix_web_service/src/models/lib.rs | pub mod course;
| rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/bin/quick-start.rs | 提升篇/actix_web_service/src/bin/quick-start.rs | use actix_web::HttpResponse;
use actix_web::{web, App, HttpServer};
use std::io;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug,Serialize,Deserialize)]
struct Hello{
msg:String
}
#[actix_rt::main]
async fn main()-> io::Result<()>{
HttpServer::new(move ||{
App::new().service(web::resourc... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/bin/teacher-service.rs | 提升篇/actix_web_service/src/bin/teacher-service.rs | use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
use sqlx::postgres::PgPoolOptions;
use std::sync::Mutex;
use std::{env, io};
#[path = "../handlers/lib.rs"]
mod handlers;
#[path = "../routers.rs"]
mod routers;
#[path = "../state.rs"]
mod state;
#[path = "../models/lib.rs"]
mod models;
#[path = "../db_acc... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/handlers/course.rs | 提升篇/actix_web_service/src/handlers/course.rs | use crate::db_access::course::*;
use crate::error::MyError;
use crate::models::course::{CreateCourse, UpdateCourse};
use crate::state::AppState;
use actix_web::{web, HttpResponse};
pub async fn post_new_course(
new_course: web::Json<CreateCourse>,
app_state: web::Data<AppState>,
) -> Result<HttpResponse, MyErr... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/handlers/general.rs | 提升篇/actix_web_service/src/handlers/general.rs | use crate::state::AppState;
use actix_web::{web, HttpResponse};
pub async fn health_check_handler(app_state: web::Data<AppState>) -> HttpResponse {
println!("incoming for health check");
let health_check_response = &app_state.health_check_response;
let mut visit_count = app_state.visit_count.lock().unwrap(... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
reganzm/hug_rust | https://github.com/reganzm/hug_rust/blob/6a2801769c8b062477c2e6273af0218e0e21e75d/提升篇/actix_web_service/src/handlers/lib.rs | 提升篇/actix_web_service/src/handlers/lib.rs | pub mod course;
pub mod general;
| 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_stack_decimal_conversion.rs | 数据结构和算法/linear_data_structure/src/bin/use_stack_decimal_conversion.rs | //!使用栈进行16进制以内的任意进制转换
use stack::Stack;
mod stack;
fn decimal_conversion(mut origin_num: u32, base: u32) -> String {
let digits = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F',
];
// 使用转辗相除法
let mut stack = Stack::new();
// 余数入栈
while origin_nu... | 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_stack_par_checker.rs | 数据结构和算法/linear_data_structure/src/bin/use_stack_par_checker.rs | //! 使用栈进行"括号"匹配
//! 这里的"括号"包括[]、{}、()
use std::vec;
use crate::stack::Stack;
mod stack;
/// 判断是否匹配
fn matcher(left: char, right: char) -> bool {
let lefts = "([{";
let rights = ")]}";
lefts.find(left) == rights.find(right)
}
// 使用栈判断括号是否匹配
fn par_checher(input: &str) -> bool {
if input.is_empty() {
... | 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_queue_josephus_circle.rs | 数据结构和算法/linear_data_structure/src/bin/use_queue_josephus_circle.rs | //! 约瑟夫环(最后剩下谁,烫手的山芋游戏)
mod queue;
use queue::Queue;
fn josephus_circle(names: Vec<&str>, num: usize) -> &str {
// 将参与游戏的名字入队列
let mut q = Queue::new(names.len());
for name in names {
let _i = q.enqueue(name);
}
while q.len() > 1 {
// 数num-1次,相当于做num-1次出队入队操作,第num次的人将被移除
for... | 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/stack.rs | 数据结构和算法/linear_data_structure/src/bin/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/数据结构和算法/linear_data_structure/src/bin/dequeue.rs | 数据结构和算法/linear_data_structure/src/bin/dequeue.rs | //! 双端队列
#[derive(Debug)]
pub struct Dequeue<T> {
cap: usize,
datas: Vec<T>,
}
impl<T> Dequeue<T> {
pub fn new(cap: usize) -> Self {
Self {
cap: cap,
datas: Vec::with_capacity(cap),
}
}
pub fn is_empty(&self) -> bool {
self.datas.len() == 0
}
... | 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/lstack.rs | 数据结构和算法/linear_data_structure/src/bin/lstack.rs | //!基于链表实现的栈数据结构
mod linklist;
use std::fmt::{Debug, Display};
// 节点引用类型
type NodeRef<T> = Option<Box<Node<T>>>;
// 链表节点
#[derive(Debug)]
struct Node<T> {
// 数据
elem: T,
// 下一个节点
next: NodeRef<T>,
}
impl<T: Debug + Display> Node<T> {
fn new(data: T) -> Self {
Self {
elem: data,... | 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/linklist.rs | 数据结构和算法/linear_data_structure/src/bin/linklist.rs | //! 链表
use std::fmt::Debug;
// 节点引用类型
type NodeRef<T> = Option<Box<Node<T>>>;
#[derive(Debug)]
struct List<T: Debug> {
// 链表大小
size: usize,
// 头结点
head: NodeRef<T>,
}
// 链表节点
#[derive(Debug)]
struct Node<T> {
// 数据
elem: T,
// 下一个节点
next: NodeRef<T>,
}
impl<T: Debug> List<T> {
fn n... | 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_list_create_vec.rs | 数据结构和算法/linear_data_structure/src/bin/use_list_create_vec.rs | //!使用链表创建vec
use std::fmt::Debug;
type NodeRef<T> = Option<Box<Node<T>>>;
#[derive(Debug)]
struct Node<T> {
ele: T,
next: NodeRef<T>,
}
impl<T> Node<T> {
fn new(ele: T) -> Self {
Self {
ele: ele,
next: None,
}
}
}
// 基于链表实现的Vec
#[derive(Debug)]
struct LVec<T> ... | rust | Apache-2.0 | 6a2801769c8b062477c2e6273af0218e0e21e75d | 2026-01-04T20:24:35.074754Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.