بنقرة واحدة
rust
当进行Rust系统编程时,分析内存管理,优化并发性能,解决安全问题。验证系统架构,设计高性能应用,和最佳实践。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
当进行Rust系统编程时,分析内存管理,优化并发性能,解决安全问题。验证系统架构,设计高性能应用,和最佳实践。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
以聚合根为边界,包含多个相关Entity和ValueObject的集合。保证数据一致性和事务边界。
在DDD中具有唯一身份标识和生命周期的对象,通过身份而非属性值相等判断。
封装复杂对象和聚合的创建过程,将创建职责从领域对象中剥离,保证聚合创建时的不变量满足。
没有身份标识,通过属性值判断相等的对象。不可变,通常代表领域中的度量或描述。
命令查询责任分离,将数据的写入操作和读取操作分别用不同的模型处理,优化各自的性能。
将DDD战略设计应用于微服务架构,限界上下文指导服务拆分,领域事件实现服务间通信。
| name | Rust系统编程 |
| description | 当进行Rust系统编程时,分析内存管理,优化并发性能,解决安全问题。验证系统架构,设计高性能应用,和最佳实践。 |
| license | MIT |
Rust是一门系统编程语言,以其内存安全、并发安全和高性能而著称。Rust通过所有权系统、借用检查器和生命周期机制,在编译时就能防止许多常见的编程错误。不当的Rust编程会导致编译错误、性能问题、代码复杂。
核心原则: 好的Rust代码应该内存安全、并发安全、性能优良、可读性强。坏的Rust代码会滥用unsafe、性能损耗、难以维护。
始终:
触发短语:
问题: 借用检查器错误
原因: 不理解Rust的借用规则
解决: 学习所有权和借用机制,使用引用和克隆
问题: 生命周期错误
原因: 生命周期标注不正确
解决: 理解生命周期规则,使用生命周期标注
问题: 过度克隆导致性能下降
原因: 不理解所有权转移
解决: 合理使用引用,避免不必要的克隆
问题: 频繁的内存分配
原因: 不了解Rust的内存管理
解决: 使用栈分配、对象池等技术
问题: 数据竞争
原因: 不正确的共享数据访问
解决: 使用Rust的并发安全机制
问题: 死锁
原因: 锁的获取顺序不当
解决: 遵循一致的锁获取顺序
// 基础所有权概念
fn ownership_basics() {
// 字符串所有权转移
let s1 = String::from("Hello");
let s2 = s1; // 所有权从 s1 转移到 s2
// println!("{}", s1); // 编译错误:s1 不再拥有字符串
println!("{}", s2); // 正确:s2 拥有字符串
// 克隆避免所有权转移
let s3 = String::from("World");
let s4 = s3.clone(); // 克隆数据,s3 仍然有效
println!("{}, {}", s3, s4); // 正确:两个都有效
// 函数参数的所有权转移
let s5 = String::from("Rust");
takes_ownership(s5); // s5 的所有权转移到函数
// println!("{}", s5); // 编译错误:s5 不再有效
let s6 = String::from("Programming");
let len = calculate_length(&s6); // 借用,不转移所有权
println!("'{}' 的长度是 {}", s6, len); // s6 仍然有效
}
fn takes_ownership(some_string: String) {
println!("{}", some_string);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
// 可变借用
fn mutable_borrowing() {
let mut s = String::from("Hello");
// 不可变借用
let r1 = &s;
let r2 = &s;
println!("r1: {}, r2: {}", r1, r2);
// 可变借用(在不可变借用作用域结束后)
let r3 = &mut s;
r3.push_str(", World!");
println!("r3: {}", r3);
// 悬垂引用示例(编译错误)
// let reference_to_nothing = dangle();
}
// fn dangle() -> &String { // 编译错误
// let s = String::from("Hello");
// &s // s 将在函数结束时被销毁
// }
// 切片类型
fn slice_types() {
let s = String::from("Hello World");
// 字符串切片
let hello = &s[0..5];
let world = &s[6..11];
println!("{} {}", hello, world);
// 数组切片
let a = [1, 2, 3, 4, 5];
let slice = &a[1..4];
println!("数组切片: {:?}", slice);
// 函数参数使用切片
let word = first_word(&s);
println!("第一个单词: {}", word);
}
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
// 结构体定义
#[derive(Debug, Clone, Copy)]
struct Point {
x: f64,
y: f64,
}
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
top_left: Point,
}
impl Rectangle {
// 关联函数(静态方法)
fn new(width: u32, height: u32) -> Self {
Rectangle {
width,
height,
top_left: Point { x: 0.0, y: 0.0 },
}
}
// 方法
fn area(&self) -> u32 {
self.width * self.height
}
// 可变方法
fn resize(&mut self, new_width: u32, new_height: u32) {
self.width = new_width;
self.height = new_height;
}
// 获取所有权的方法
fn move_to(self, new_x: f64, new_y: f64) -> Self {
Rectangle {
width: self.width,
height: self.height,
top_left: Point { x: new_x, y: new_y },
}
}
}
// 枚举定义
#[derive(Debug)]
enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
}
#[derive(Debug)]
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn process(&self) {
match self {
Message::Quit => println!("退出程序"),
Message::Move { x, y } => println!("移动到坐标 ({}, {})", x, y),
Message::Write(text) => println!("写入文本: {}", text),
Message::ChangeColor(r, g, b) => println!("改变颜色为 RGB({}, {}, {})", r, g, b),
}
}
}
// Option 和 Result 类型
fn option_examples() {
let numbers = vec![1, 2, 3, 4, 5];
// 使用 Option
let first_even = numbers.iter().find(|&&x| x % 2 == 0);
match first_even {
Some(num) => println!("第一个偶数: {}", num),
None => println!("没有找到偶数"),
}
// 使用 Option 的方法
let doubled = first_even.map(|x| x * 2);
println!("偶数的两倍: {:?}", doubled);
// 使用 unwrap_or 提供默认值
let value = doubled.unwrap_or(0);
println!("值: {}", value);
}
fn result_examples() -> Result<i32, String> {
let x = 10;
let y = 0;
if y == 0 {
Err("除数不能为零".to_string())
} else {
Ok(x / y)
}
}
fn handle_result() {
match result_examples() {
Ok(result) => println!("结果: {}", result),
Err(error) => println!("错误: {}", error),
}
// 使用 ? 操作符
let result = divide_numbers(10, 2);
println!("除法结果: {}", result);
let result = divide_numbers(10, 0);
println!("除法结果: {}", result);
}
fn divide_numbers(x: i32, y: i32) -> Result<i32, String> {
if y == 0 {
Err("除数不能为零".to_string())
} else {
Ok(x / y)
}
}
use std::fs;
use std::io::{self, Read};
// 自定义错误类型
#[derive(Debug)]
enum AppError {
Io(io::Error),
ParseError(std::num::ParseIntError),
Custom(String),
}
impl std::fmt::Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
AppError::Io(err) => write!(f, "IO错误: {}", err),
AppError::ParseError(err) => write!(f, "解析错误: {}", err),
AppError::Custom(msg) => write!(f, "自定义错误: {}", msg),
}
}
}
impl std::error::Error for AppError {}
// From trait 实现
impl From<io::Error> for AppError {
fn from(err: io::Error) -> Self {
AppError::Io(err)
}
}
impl From<std::num::ParseIntError> for AppError {
fn from(err: std::num::ParseIntError) -> Self {
AppError::ParseError(err)
}
}
// 错误处理函数
fn read_file_contents(path: &str) -> Result<String, AppError> {
let mut content = String::new();
let mut file = fs::File::open(path)?;
file.read_to_string(&mut content)?;
Ok(content)
}
fn parse_numbers(content: &str) -> Result<Vec<i32>, AppError> {
content
.lines()
.map(|line| line.trim().parse::<i32>())
.collect()
}
fn calculate_sum(path: &str) -> Result<i32, AppError> {
let content = read_file_contents(path)?;
let numbers = parse_numbers(&content)?;
Ok(numbers.iter().sum())
}
// 错误恢复策略
fn safe_divide(x: f64, y: f64) -> Option<f64> {
if y == 0.0 {
None
} else {
Some(x / y)
}
}
fn robust_calculation() {
let operations = vec![
(10.0, 2.0),
(5.0, 0.0),
(8.0, 4.0),
];
for (x, y) in operations {
match safe_divide(x, y) {
Some(result) => println!("{}/{} = {}", x, y, result),
None => println!("{}/{} 无法计算(除数为零)", x, y),
}
}
}
// 错误链
use std::error;
use std::fmt;
#[derive(Debug)]
struct DatabaseError {
message: String,
source: Option<Box<dyn error::Error + Send + Sync>>,
}
impl fmt::Display for DatabaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "数据库错误: {}", self.message)
}
}
impl error::Error for DatabaseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.source.as_ref().map(|e| e.as_ref())
}
}
fn database_operation() -> Result<(), DatabaseError> {
// 模拟数据库操作
Err(DatabaseError {
message: "连接失败".to_string(),
source: Some(Box::new(io::Error::new(io::ErrorKind::ConnectionRefused, "无法连接"))),
})
}
use std::thread;
use std::sync::{Arc, Mutex, Condvar};
use std::sync::mpsc;
use std::time::Duration;
// 基础线程使用
fn basic_threading() {
let handle = thread::spawn(|| {
for i in 1..=5 {
println!("线程中的数字: {}", i);
thread::sleep(Duration::from_millis(100));
}
});
for i in 1..=3 {
println!("主线程中的数字: {}", i);
thread::sleep(Duration::from_millis(100));
}
handle.join().unwrap();
}
// 通道通信
fn channel_communication() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("你好"),
String::from("来自"),
String::from("线程"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_millis(100));
}
});
for received in rx {
println!("收到: {}", received);
}
}
// 多生产者单消费者
fn multiple_producers() {
let (tx, rx) = mpsc::channel();
// 创建多个发送者
for i in 0..3 {
let tx_clone = tx.clone();
thread::spawn(move || {
for j in 0..3 {
let message = format!("线程 {} 消息 {}", i, j);
tx_clone.send(message).unwrap();
thread::sleep(Duration::from_millis(100));
}
});
}
drop(tx); // 关闭主发送者
for received in rx {
println!("收到: {}", received);
}
}
// 共享状态
fn shared_state() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter_clone = Arc::clone(&counter);
let handle = thread::spawn(move || {
for _ in 0..1000 {
let mut num = counter_clone.lock().unwrap();
*num += 1;
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("计数器最终值: {}", *counter.lock().unwrap());
}
// 条件变量
fn condition_variables() {
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair_clone = Arc::clone(&pair);
thread::spawn(move || {
let (ref lock, ref cvar) = *pair_clone;
let mut started = lock.lock().unwrap();
*started = true;
println!("通知主线程继续");
cvar.notify_one();
});
let (ref lock, ref cvar) = *pair;
let mut started = lock.lock().unwrap();
while !*started {
started = cvar.wait(started).unwrap();
}
println!("主线程继续执行");
}
// 原子操作
use std::sync::atomic::{AtomicUsize, Ordering};
fn atomic_operations() {
let counter = AtomicUsize::new(0);
let mut handles = vec![];
for _ in 0..10 {
let counter_clone = counter.clone();
let handle = thread::spawn(move || {
for _ in 0..1000 {
counter_clone.fetch_add(1, Ordering::SeqCst);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("原子计数器最终值: {}", counter.load(Ordering::SeqCst));
}
use tokio;
use tokio::time::{sleep, Duration};
use futures::future::join_all;
// 基础异步函数
async fn say_hello() {
println!("Hello");
sleep(Duration::from_millis(100)).await;
println!("World");
}
// 异步函数返回值
async fn calculate_sum(a: i32, b: i32) -> i32 {
sleep(Duration::from_millis(50)).await;
a + b
}
// 异步迭代
async fn process_numbers() {
let numbers = vec![1, 2, 3, 4, 5];
for num in numbers {
let result = async_process(num).await;
println!("处理结果: {}", result);
}
}
async fn async_process(num: i32) -> i32 {
sleep(Duration::from_millis(100)).await;
num * 2
}
// 并发异步任务
async fn concurrent_tasks() {
let task1 = async_task(1, 200);
let task2 = async_task(2, 100);
let task3 = async_task(3, 150);
// 并发执行
let (result1, result2, result3) = tokio::join!(task1, task2, task3);
println!("并发结果: {}, {}, {}", result1, result2, result3);
}
async fn async_task(id: u32, delay_ms: u64) -> String {
sleep(Duration::from_millis(delay_ms)).await;
format!("任务 {} 完成", id)
}
// 动态并发任务
async fn dynamic_concurrent() {
let tasks: Vec<_> = (1..=5)
.map(|i| async_task(i, i * 50))
.collect();
let results = join_all(tasks).await;
for result in results {
println!("动态任务结果: {}", result);
}
}
// 异步错误处理
async fn async_operation(success: bool) -> Result<String, String> {
sleep(Duration::from_millis(100)).await;
if success {
Ok("操作成功".to_string())
} else {
Err("操作失败".to_string())
}
}
async fn handle_async_errors() {
match async_operation(true).await {
Ok(result) => println!("成功: {}", result),
Err(error) => println!("失败: {}", error),
}
// 使用 ? 操作符
let result = async_operation_wrapper().await;
println!("包装器结果: {:?}", result);
}
async fn async_operation_wrapper() -> Result<String, String> {
let result = async_operation(true).await?;
Ok(format!("包装后的结果: {}", result))
}
// 异步流处理
use futures::stream::{self, StreamExt};
async fn stream_processing() {
let numbers = stream::iter(1..=10);
let processed = numbers
.map(|n| async move {
sleep(Duration::from_millis(50)).await;
n * 2
})
.buffer_unordered(3); // 并发处理3个任务
processed.for_each(|result| async move {
println!("流处理结果: {}", result);
}).await;
}
// 异步通道
use tokio::sync::mpsc;
async fn async_channels() {
let (tx, mut rx) = mpsc::channel(32);
// 发送者任务
tokio::spawn(async move {
for i in 1..=5 {
let message = format!("消息 {}", i);
tx.send(message).await.unwrap();
sleep(Duration::from_millis(100)).await;
}
});
// 接收者
while let Some(message) = rx.recv().await {
println!("收到异步消息: {}", message);
}
}
// 运行异步示例
#[tokio::main]
async fn main() {
say_hello().await;
let sum = calculate_sum(10, 20).await;
println!("异步计算结果: {}", sum);
process_numbers().await;
concurrent_tasks().await;
dynamic_concurrent().await;
handle_async_errors().await;
stream_processing().await;
async_channels().await;
}
use std::rc::Rc;
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
// Box<T> - 堆分配
fn box_example() {
let b = Box::new(5);
println!("b = {}", b);
// 递归类型需要 Box
#[derive(Debug)]
enum List {
Cons(i32, Box<List>),
Nil,
}
use List::{Cons, Nil};
let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
println!("{:?}", list);
}
// Rc<T> - 引用计数
fn rc_example() {
#[derive(Debug)]
struct Node {
value: i32,
children: RefCell<Vec<Rc<Node>>>,
}
let leaf = Rc::new(Node {
value: 3,
children: RefCell::new(vec![]),
});
{
let branch = Rc::new(Node {
value: 5,
children: RefCell::new(vec![Rc::clone(&leaf)]),
});
leaf.children.borrow_mut().push(Rc::clone(&branch));
}
println!("leaf 的强引用计数: {}", Rc::strong_count(&leaf));
}
// Arc<T> - 原子引用计数
fn arc_example() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter_clone = Arc::clone(&counter);
let handle = thread::spawn(move || {
for _ in 0..1000 {
let mut num = counter_clone.lock().unwrap();
*num += 1;
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Arc 计数器最终值: {}", *counter.lock().unwrap());
}
// 自定义智能指针
struct MyBox<T>(T);
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}
impl<T> std::ops::Deref for MyBox<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn custom_smart_pointer() {
let x = 5;
let y = MyBox::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
assert_eq!(5, *(y.deref()));
}
// Drop trait
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("删除 CustomSmartPointer,数据为: {}", self.data);
}
}
fn drop_trait_example() {
let _c1 = CustomSmartPointer {
data: String::from("我的数据"),
};
let _c2 = CustomSmartPointer {
data: String::from("其他数据"),
};
println!("CustomSmartPointers 创建完成");
// c1 和 c2 在这里被自动删除
}