【quantity】0 README.md文件

PhysUnits · 物理单位库

Type-safe physical quantities with dimensional analysis
带量纲分析的类型安全物理量库

A Rust library for safe unit operations /

Rust实现的类型安全单位计算库

Core Design / 核心设计

1. Dimension / 量纲

rust 复制代码
/// Base SI dimensions / 国际单位制基本量纲
pub struct Dimension<
    L: Integer,  // Length (m) / 长度(米)
    M: Integer,  // Mass (kg) / 质量(千克)
    T: Integer,  // Time (s) / 时间(秒)
    I: Integer,  // Current (A) / 电流(安培)
    Th: Integer, // Temperature (K) / 温度(开尔文) 
    N: Integer,  // Amount (mol) / 物质的量(摩尔)
    J: Integer   // Luminosity (cd) / 发光强度(坎德拉)
>(PhantomData<(L, M, T, I, Th, N, J)>);

2. Unit / 单位

rust 复制代码
/// Unit conversion rules / 单位转换规则
pub trait Unit {
    type Dimension;
    
    /// Convert to base unit / 转换到基准单位
    fn to_base(value: f64) -> f64;
    
    /// Unit symbol / 单位符号
    const SYMBOL: &'static str;
}

3. Quantity / 物理量

rust 复制代码
/// Physical quantity with value and unit / 带单位和值的物理量
pub struct Quantity<V, U: Unit> {
    /// Scalar value / 标量值
    pub value: V,
    _unit: PhantomData<U>
}

impl<V, U: Unit> Quantity<V, U> {
    /// Create new quantity / 创建新物理量
    pub fn new(value: V) -> Self {
        Self { value, _unit: PhantomData }
    }
}

Usage / 使用示例

Basic Conversion / 基础转换

rust 复制代码
use physunits::{Meter, Inch, Quantity};

// Create length / 创建长度
let length = Quantity::<f64, Meter>::new(2.0);

// Convert units / 单位转换
let inches = length.convert::<Inch>();
println!("{} m = {} in", length.value, inches.value);

Temperature / 温度转换

rust 复制代码
use physunits::{Celsius, Fahrenheit};

let boiling = Quantity::<f64, Celsius>::new(100.0);
let fahr = boiling.convert::<Fahrenheit>();
println!("Water boils at {} °F", fahr.value); 

Force Calculation / 力的计算

rust 复制代码
use physunits::{kg, m, s, N};

let mass = 5.0 * kg;
let acceleration = 9.8 * m / (s * s);
let force: Quantity<f64, N> = mass * acceleration;
println!("Force: {} N", force.value);

Features / 特性

Feature 功能描述
📏 Compile-time dimensional safety 编译期量纲安全
⚡ Zero runtime overhead 零运行时开销
🔢 Integer & float support 支持整数和浮点数
🔄 Automatic unit conversion 自动单位转换

Installation / 安装

toml 复制代码
[dependencies]
physunits = "0.0.1"
相关推荐
superman超哥23 分钟前
Rust 枚举与结构体定义:类型系统的代数基石
rust·类型系统·rust枚举与结果体定义·代数基石
superman超哥2 小时前
Rust 基本数据类型:类型安全的底层探索
开发语言·rust·rust基本数据类型·rust底层探索·类型安全
苏近之3 小时前
Rust 中实现定时任务管理
后端·架构·rust
该用户已不存在3 小时前
7个构建高性能后端的 Rust 必备库
后端·rust
superman超哥7 小时前
Rust impl 块的组织方式:模块化设计的艺术
开发语言·后端·rust·模块化设计·rust impl块·impl块
superman超哥9 小时前
Rust 表达式与语句的区别:函数式思维与控制流设计
开发语言·后端·rust·rust表达式·rust语句·函数式思维·控制流设计
superman超哥9 小时前
Rust Trait 定义与实现:类型系统的多态基石
开发语言·rust·类型系统·rust trait·定义与实现·多态基石
superman超哥9 小时前
Rust 方法与关联函数:所有权语义下的行为设计
开发语言·rust·rust底层探索·rust方法与关联函数·所有权语义下的行为设计
superman超哥10 小时前
Rust 复合类型:元组与数组的内存布局与性能优化
开发语言·后端·性能优化·rust·内存布局·rust复合类型·元组与数组
superman超哥10 小时前
Rust 函数定义与参数传递:所有权系统下的设计艺术
开发语言·rust·设计艺术·rust函数定义·rust参数传递