Skip to main content

Velocity

Struct Velocity 

Source
pub struct Velocity { /* private fields */ }
Expand description

速度物理量结构体

表示物体运动的快慢和方向,支持多种单位制

§示例

use zmatrix::physics::basic::Velocity;

let velocity = Velocity::from_m_per_sec(10.0);
println!("速度: {} m/s", velocity.as_m_per_sec());

Implementations§

Source§

impl Velocity

Source

pub fn from_m_per_sec(v: f64) -> Self

Examples found in repository?
examples/example_physics.rs (line 11)
4fn main(){
5    let distance = Distance::from_m(1001.0);
6    println!("{:?}", distance.as_km());
7    // 1.001 km
8    println!("{:?}", distance.as_light_year());
9    // 1.05805783485864e-13 light years(1001 m ÷ 9.4607e15 m/ly)
10
11    let velocity = Velocity::from_m_per_sec(100.0);
12    println!("{:?}", velocity.as_km_per_h());
13    // 360.0 km/h
14
15    calculate();
16    examples_vector3();
17}
More examples
Hide additional examples
examples/example_vector_quantity.rs (line 11)
3fn main() {
4    println!("=== VectorQuantity Trait 验证示例 ===\n");
5
6    // 验证支持向量的物理量
7    println!("1. 验证支持向量的物理量:");
8    
9    // 创建一些支持向量的物理量
10    let distance = Distance::from_m(10.0);
11    let velocity = Velocity::from_m_per_sec(5.0);
12    let force = Force::from_newton(20.0);
13    let torque = Torque::from_nm(15.0);
14    let magnetic_induction = MagneticInduction::from_tesla(0.5);
15
16    println!("   Distance: {} m", distance.as_m());
17    println!("   Velocity: {} m/s", velocity.as_m_per_sec());
18    println!("   Force: {} N", force.as_newton());
19    println!("   Torque: {} N·m", torque.as_nm());
20    println!("   MagneticInduction: {} T", magnetic_induction.as_tesla());
21
22    // 验证这些类型都实现了 VectorQuantity trait
23    println!("\n2. 验证 VectorQuantity trait 实现:");
24    
25    // 这些函数调用会验证类型是否实现了 VectorQuantity trait
26    check_vector_quantity(distance);
27    check_vector_quantity(velocity);
28    check_vector_quantity(force);
29    check_vector_quantity(torque);
30    check_vector_quantity(magnetic_induction);
31
32    println!("   ✓ 所有物理量都正确实现了 VectorQuantity trait");
33
34    // 创建 Vector3 实例来验证向量操作
35    println!("\n3. 验证 Vector3 操作:");
36    
37    let distance_vector = Vector3::new(
38        Distance::from_m(1.0),
39        Distance::from_m(2.0),
40        Distance::from_m(3.0)
41    );
42    
43    let velocity_vector = Vector3::new(
44        Velocity::from_m_per_sec(4.0),
45        Velocity::from_m_per_sec(5.0),
46        Velocity::from_m_per_sec(6.0)
47    );
48
49    println!("   Distance Vector: ({}, {}, {}) m", 
50             distance_vector.x.as_m(), 
51             distance_vector.y.as_m(), 
52             distance_vector.z.as_m());
53    
54    println!("   Velocity Vector: ({}, {}, {}) m/s", 
55             velocity_vector.x.as_m_per_sec(), 
56             velocity_vector.y.as_m_per_sec(), 
57             velocity_vector.z.as_m_per_sec());
58
59    // 验证向量运算 - 只能对相同类型的向量进行运算
60    let sum_distance = distance_vector + distance_vector;
61    println!("   Sum Distance Vector: ({}, {}, {}) m", 
62             sum_distance.x.as_m(), 
63             sum_distance.y.as_m(), 
64             sum_distance.z.as_m());
65
66    println!("\n=== 验证完成 ===");
67}
examples/example_vector_quantity_restriction.rs (line 20)
3fn main() {
4    println!("=== Vector3 类型约束验证示例 ===\n");
5
6    // 1. 验证支持向量的物理量可以用于 Vector3
7    println!("1. 支持向量的物理量可以用于 Vector3:");
8    
9    let distance_vector = Vector3::new(
10        Distance::from_m(1.0),
11        Distance::from_m(2.0),
12        Distance::from_m(3.0)
13    );
14    println!("   ✓ Distance Vector: ({}, {}, {}) m", 
15             distance_vector.x.as_m(), 
16             distance_vector.y.as_m(), 
17             distance_vector.z.as_m());
18
19    let velocity_vector = Vector3::new(
20        Velocity::from_m_per_sec(4.0),
21        Velocity::from_m_per_sec(5.0),
22        Velocity::from_m_per_sec(6.0)
23    );
24    println!("   ✓ Velocity Vector: ({}, {}, {}) m/s", 
25             velocity_vector.x.as_m_per_sec(), 
26             velocity_vector.y.as_m_per_sec(), 
27             velocity_vector.z.as_m_per_sec());
28
29    let force_vector = Vector3::new(
30        Force::from_newton(10.0),
31        Force::from_newton(20.0),
32        Force::from_newton(30.0)
33    );
34    println!("   ✓ Force Vector: ({}, {}, {}) N", 
35             force_vector.x.as_newton(), 
36             force_vector.y.as_newton(), 
37             force_vector.z.as_newton());
38
39    // 2. 验证不支持向量的物理量无法用于 Vector3
40    println!("\n2. 不支持向量的物理量无法用于 Vector3:");
41    println!("   ✓ 以下代码会在编译时被拒绝:");
42    println!("     let mass_vector = Vector3::new(");
43    println!("         Mass::from_kg(1.0),");
44    println!("         Mass::from_kg(2.0),");
45    println!("         Mass::from_kg(3.0)");
46    println!("     );");
47    println!("\n   ✓ 以下代码会在编译时被拒绝:");
48    println!("     let energy_vector = Vector3::new(");
49    println!("         Energy::from_joule(1.0),");
50    println!("         Energy::from_joule(2.0),");
51    println!("         Energy::from_joule(3.0)");
52    println!("     );");
53
54    // 3. 列出所有支持向量的物理量
55    println!("\n3. 支持向量的物理量列表:");
56    println!("   ✓ Distance (距离/位移)");
57    println!("   ✓ Velocity (速度)");
58    println!("   ✓ Acceleration (加速度)");
59    println!("   ✓ Angular (角度)");
60    println!("   ✓ AngularVelocity (角速度)");
61    println!("   ✓ AngularAcceleration (角加速度)");
62    println!("   ✓ Coef (系数)");
63    println!("   ✓ AngularMomentum (角动量)");
64    println!("   ✓ Momentum (动量)");
65    println!("   ✓ MagneticInduction (磁感应强度)");
66    println!("   ✓ MagneticMoment (磁矩)");
67    println!("   ✓ Torque (力矩)");
68    println!("   ✓ Force (力)");
69    println!("   ✓ MagneticAngularVelocity (磁角速度)");
70
71    // 4. 列出不支持向量的物理量
72    println!("\n4. 不支持向量的物理量列表:");
73    println!("   ✗ Mass (质量)");
74    println!("   ✗ Energy (能量)");
75    println!("   ✗ Power (功率)");
76    println!("   ✗ Volume (体积)");
77    println!("   ✗ Area (面积)");
78    println!("   ✗ Time (时间)");
79
80    println!("\n=== 验证完成 ===");
81    println!("Vector3 现在只能用于支持向量的物理量,这确保了类型安全!");
82}
examples/example_energy.rs (line 80)
3fn main() {
4    println!("=== 能量物理量示例 ===\n");
5
6    // 1. 创建不同单位的能量
7    println!("1. 创建不同单位的能量:");
8    let e1 = Energy::from_joule(1.0); // 1 J
9    let e2 = Energy::from_mill_joule(1000.0); // 1000 mJ = 1 J
10    let e3 = Energy::from_micro_joule(1e6); // 1e6 μJ = 1 J
11    let e4 = Energy::from_nano_joule(1e9); // 1e9 nJ = 1 J
12    let e5 = Energy::from_kilo_joule(1e-3); // 1e-3 kJ = 1 J
13    let e6 = Energy::from_mega_joule(1e-6); // 1e-6 MJ = 1 J
14    let e7 = Energy::from_electron_volt(1.0 / 1.602176634e-19); // 1 J
15    let e8 = Energy::from_kilo_electron_volt(1.0 / 1.602176634e-16); // 1 J
16    let e9 = Energy::from_mega_electron_volt(1.0 / 1.602176634e-13); // 1 J
17
18    println!("   e1 = {} J", e1.as_joule());
19    println!("   e2 = {} mJ = {} J", e2.as_mill_joule(), e2.as_joule());
20    println!("   e3 = {} μJ = {} J", e3.as_micro_joule(), e3.as_joule());
21    println!("   e4 = {} nJ = {} J", e4.as_nano_joule(), e4.as_joule());
22    println!("   e5 = {} kJ = {} J", e5.as_kilo_joule(), e5.as_joule());
23    println!("   e6 = {} MJ = {} J", e6.as_mega_joule(), e6.as_joule());
24    println!("   e7 = {} eV = {} J", e7.as_electron_volt(), e7.as_joule());
25    println!("   e8 = {} keV = {} J", e8.as_kilo_electron_volt(), e8.as_joule());
26    println!("   e9 = {} MeV = {} J", e9.as_mega_electron_volt(), e9.as_joule());
27
28    // 2. 单位转换
29    println!("\n2. 单位转换:");
30    println!("   1 J = {} mJ", e1.as_mill_joule());
31    println!("   1 J = {} μJ", e1.as_micro_joule());
32    println!("   1 J = {} nJ", e1.as_nano_joule());
33    println!("   1 J = {} kJ", e1.as_kilo_joule());
34    println!("   1 J = {} MJ", e1.as_mega_joule());
35    println!("   1 J = {} eV", e1.as_electron_volt());
36    println!("   1 J = {} keV", e1.as_kilo_electron_volt());
37    println!("   1 J = {} MeV", e1.as_mega_electron_volt());
38
39    // 3. 能量运算
40    println!("\n3. 能量运算:");
41    let e_sum = e1 + e2; // 1 J + 1 J = 2 J
42    let e_diff = e_sum - e1; // 2 J - 1 J = 1 J
43    let e_scale = e1 * 3.0; // 1 J * 3 = 3 J
44    let e_div = e_scale / 2.0; // 3 J / 2 = 1.5 J
45
46    println!("   e1 + e2 = {} J", e_sum.as_joule());
47    println!("   e_sum - e1 = {} J", e_diff.as_joule());
48    println!("   e1 * 3 = {} J", e_scale.as_joule());
49    println!("   e_scale / 2 = {} J", e_div.as_joule());
50
51    // 4. 不同单位之间的运算
52    println!("\n4. 不同单位之间的运算:");
53    let e_micro = Energy::from_micro_joule(1e6); // 1 J
54    let e_mill = Energy::from_mill_joule(1000.0); // 1 J
55    let e_result = e_micro + e_mill; // 1 J + 1 J = 2 J
56    println!("   {} μJ + {} mJ = {} J", 
57             e_micro.as_micro_joule(), 
58             e_mill.as_mill_joule(), 
59             e_result.as_joule());
60
61    // 5. 标量与能量的运算
62    println!("\n5. 标量与能量的运算:");
63    let e_scaled = 5.0 * e1; // 5 × 1 J = 5 J
64    let e_divided = 10.0 / e1; // 10 / 1 J = 10 J
65    println!("   5.0 * {} J = {} J", e1.as_joule(), e_scaled.as_joule());
66    println!("   10.0 / {} J = {} J", e1.as_joule(), e_divided.as_joule());
67
68    // 6. 零值检查
69    println!("\n6. 零值检查:");
70    let e_zero = Energy::from_joule(0.0);
71    let e_nonzero = Energy::from_joule(1.0);
72    println!("   e_zero.is_zero() = {}", e_zero.is_zero());
73    println!("   e_nonzero.is_zero() = {}", e_nonzero.is_zero());
74
75    // 7. 实际应用示例
76    println!("\n7. 实际应用示例:");
77    
78    // 动能计算
79    let mass = Mass::from_kg(2.0); // 2 kg
80    let velocity = Velocity::from_m_per_sec(3.0); // 3 m/s
81    let kinetic_energy = Energy::from_joule(0.5 * mass.as_kg() * velocity.as_m_per_sec().powi(2));
82    println!("   质量: {} kg", mass.as_kg());
83    println!("   速度: {} m/s", velocity.as_m_per_sec());
84    println!("   动能: {} J", kinetic_energy.as_joule());
85
86    // 势能计算
87    let mass = Mass::from_kg(1.0); // 1 kg
88    let height = Distance::from_m(10.0); // 10 m
89    let g = 9.81; // 重力加速度 m/s²
90    let potential_energy = Energy::from_joule(mass.as_kg() * g * height.as_m());
91    println!("   质量: {} kg", mass.as_kg());
92    println!("   高度: {} m", height.as_m());
93    println!("   势能: {} J", potential_energy.as_joule());
94
95    // 电子伏特应用
96    let photon_energy = Energy::from_electron_volt(2.5); // 2.5 eV
97    println!("   光子能量: {} eV = {} J", 
98             photon_energy.as_electron_volt(), 
99             photon_energy.as_joule());
100
101    let nuclear_energy = Energy::from_mega_electron_volt(1.0); // 1 MeV
102    println!("   核能: {} MeV = {} J", 
103             nuclear_energy.as_mega_electron_volt(), 
104             nuclear_energy.as_joule());
105
106    println!("\n=== 示例完成 ===");
107}
examples/example_new_operations.rs (line 25)
4fn main() {
5    println!("=== 新增物理量运算关系示例 ===\n");
6
7    // 1. 能量 ÷ 时间 = 功率
8    println!("1. 能量 ÷ 时间 = 功率");
9    let energy = Energy::from_joule(1000.0); // 1000 J
10    let time = TimeDelta::seconds(10); // 10 s
11    let power: Power = energy / time; // 100 W
12    println!("   1000 J ÷ 10 s = {} W", power.as_watt());
13    println!("   验证: {} W × 10 s = {} J\n", power.as_watt(), (power * time).as_joule());
14
15    // 2. 力矩 ÷ 距离 = 力
16    println!("2. 力矩 ÷ 距离 = 力");
17    let torque = Torque::from_nm(50.0); // 50 N·m
18    let distance = Distance::from_m(5.0); // 5 m
19    let force: Force = torque / distance; // 10 N
20    println!("   50 N·m ÷ 5 m = {} N", force.as_newton());
21    println!("   验证: {} N × 5 m = {} J\n", force.as_newton(), (force * distance).as_joule());
22
23    // 3. 速度 ÷ 加速度 = 时间
24    println!("3. 速度 ÷ 加速度 = 时间");
25    let velocity = Velocity::from_m_per_sec(20.0); // 20 m/s
26    let acceleration = Acceleration::from_m_per_s2(4.0); // 4 m/s²
27    let time = velocity / acceleration; // 5 s
28    println!("   20 m/s ÷ 4 m/s² = {} s", time_delta_to_secs_f64(&time));
29    println!("   验证: 4 m/s² × {} s = {} m/s\n", time_delta_to_secs_f64(&time), (acceleration * time).as_m_per_sec());
30
31    // 4. 距离 ÷ 速度 = 时间
32    println!("4. 距离 ÷ 速度 = 时间");
33    let distance = Distance::from_m(200.0); // 200 m
34    let velocity = Velocity::from_m_per_sec(25.0); // 25 m/s
35    let time = distance / velocity; // 8 s
36    println!("   200 m ÷ 25 m/s = {} s", time_delta_to_secs_f64(&time));
37    println!("   验证: 25 m/s × {} s = {} m\n", time_delta_to_secs_f64(&time), (velocity * time).as_m());
38
39    // 5. 功率 ÷ 力 = 速度
40    println!("5. 功率 ÷ 力 = 速度");
41    let power = Power::from_watt(300.0); // 300 W
42    let force = Force::from_newton(15.0); // 15 N
43    let velocity: Velocity = power / force; // 20 m/s
44    println!("   300 W ÷ 15 N = {} m/s", velocity.as_m_per_sec());
45    println!("   验证: 15 N × {} m/s = {} W\n", velocity.as_m_per_sec(), (force * velocity).as_watt());
46
47    // 6. 功率 ÷ 速度 = 力
48    println!("6. 功率 ÷ 速度 = 力");
49    let power = Power::from_watt(400.0); // 400 W
50    let velocity = Velocity::from_m_per_sec(16.0); // 16 m/s
51    let force: Force = power / velocity; // 25 N
52    println!("   400 W ÷ 16 m/s = {} N", force.as_newton());
53    println!("   验证: {} N × 16 m/s = {} W\n", force.as_newton(), (force * velocity).as_watt());
54
55    // 7. 角速度 ÷ 角加速度 = 时间
56    println!("7. 角速度 ÷ 角加速度 = 时间");
57    let omega = AngularVelocity::from_rad_per_second(12.0); // 12 rad/s
58    let alpha = AngularAcceleration::from_rad_per_second2(3.0); // 3 rad/s²
59    let time = omega / alpha; // 4 s
60    println!("   12 rad/s ÷ 3 rad/s² = {} s", time_delta_to_secs_f64(&time));
61    println!("   验证: 3 rad/s² × {} s = {} rad/s\n", time_delta_to_secs_f64(&time), (alpha * time).as_rad_per_second());
62
63    // 实际应用示例
64    println!("=== 实际应用示例 ===");
65    
66    // 汽车制动距离计算
67    println!("\n汽车制动距离计算:");
68    let initial_velocity = Velocity::from_km_per_h(60.0); // 60 km/h
69    let deceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
70    let braking_time = initial_velocity / deceleration; // 制动时间
71    let braking_distance = initial_velocity * braking_time; // 制动距离
72    println!("   初始速度: {} km/h", initial_velocity.as_km_per_h());
73    println!("   减速度: {} m/s²", deceleration.as_m_per_s2());
74    println!("   制动时间: {:.2} s", time_delta_to_secs_f64(&braking_time));
75    println!("   制动距离: {:.2} m", braking_distance.as_m());
76
77    // 电机功率计算
78    println!("\n电机功率计算:");
79    let torque = Torque::from_nm(100.0); // 100 N·m
80    let angular_velocity = AngularVelocity::from_rad_per_second(50.0); // 50 rad/s
81    let power: Power = torque * angular_velocity; // 5000 W
82    println!("   扭矩: {} N·m", torque.as_nm());
83    println!("   角速度: {} rad/s", angular_velocity.as_rad_per_second());
84    println!("   功率: {:.1} W ({:.2} kW)", power.as_watt(), power.as_kilo_watt());
85
86    // 杠杆原理验证
87    println!("\n杠杆原理验证:");
88    let force = Force::from_newton(20.0); // 20 N
89    let lever_arm = Distance::from_m(3.0); // 3 m
90    let energy = force * lever_arm; // 60 J
91    let calculated_force: Force = energy / lever_arm; // 20 N
92    println!("   力: {} N", force.as_newton());
93    println!("   力臂: {} m", lever_arm.as_m());
94    println!("   功: {} J", energy.as_joule());
95    println!("   验证力: {} N", calculated_force.as_newton());
96
97    println!("\n=== 示例完成 ===");
98}
examples/example_power.rs (line 67)
5fn main() {
6    println!("=== 功率物理量示例 ===\n");
7
8    // 1. 创建不同单位的功率
9    println!("1. 创建不同单位的功率:");
10    let p1 = Power::from_watt(1.0); // 1 W
11    let p2 = Power::from_mill_watt(1000.0); // 1000 mW = 1 W
12    let p3 = Power::from_micro_watt(1000000.0); // 1000000 μW = 1 W
13    let p4 = Power::from_nano_watt(1000000000.0); // 1000000000 nW = 1 W
14    let p5 = Power::from_kilo_watt(0.001); // 0.001 kW = 1 W
15    let p6 = Power::from_mega_watt(0.000001); // 0.000001 MW = 1 W
16    let p7 = Power::from_horse_power(1.0 / 745.7); // 1/745.7 hp ≈ 1 W
17
18    println!("   p1 = {} W", p1.as_watt());
19    println!("   p2 = {} mW = {} W", p2.as_mill_watt(), p2.as_watt());
20    println!("   p3 = {} μW = {} W", p3.as_micro_watt(), p3.as_watt());
21    println!("   p4 = {} nW = {} W", p4.as_nano_watt(), p4.as_watt());
22    println!("   p5 = {} kW = {} W", p5.as_kilo_watt(), p5.as_watt());
23    println!("   p6 = {} MW = {} W", p6.as_mega_watt(), p6.as_watt());
24    println!("   p7 = {} hp = {} W", p7.as_horse_power(), p7.as_watt());
25
26    // 2. 单位转换
27    println!("\n2. 单位转换:");
28    println!("   1 W = {} mW", p1.as_mill_watt());
29    println!("   1 W = {} μW", p1.as_micro_watt());
30    println!("   1 W = {} nW", p1.as_nano_watt());
31    println!("   1 W = {} kW", p1.as_kilo_watt());
32    println!("   1 W = {} MW", p1.as_mega_watt());
33    println!("   1 W = {} hp", p1.as_horse_power());
34
35    // 3. 功率的运算
36    println!("\n3. 功率的运算:");
37    let p_sum = p1 + p2; // 1 W + 1 W = 2 W
38    let p_diff = p_sum - p1; // 2 W - 1 W = 1 W
39    let p_scale = p1 * 3.0; // 1 W * 3 = 3 W
40    let p_div = p_scale / 2.0; // 3 W / 2 = 1.5 W
41
42    println!("   p1 + p2 = {} W", p_sum.as_watt());
43    println!("   p_sum - p1 = {} W", p_diff.as_watt());
44    println!("   p1 * 3 = {} W", p_scale.as_watt());
45    println!("   p_scale / 2 = {} W", p_div.as_watt());
46
47    // 4. 功率与时间的乘积(得到能量)
48    println!("\n4. 功率与时间的乘积(得到能量):");
49    let power = Power::from_watt(100.0); // 100 W
50    let time = TimeDelta::seconds(5); // 5 s
51    let energy: Energy = power * time; // 500 J
52
53    println!("   功率: {} W", power.as_watt());
54    println!("   时间: {} s", time_delta_to_secs_f64(&time));
55    println!("   能量: {} J", energy.as_joule());
56    println!("   验证: 100 W × 5 s = 500 J ✓");
57
58    // 5. 时间与功率的乘积(满足交换律)
59    println!("\n5. 时间与功率的乘积(满足交换律):");
60    let energy2: Energy = time * power; // 同样是 500 J
61    println!("   时间 × 功率 = {} J", energy2.as_joule());
62    println!("   验证: 5 s × 100 W = 500 J ✓");
63
64    // 6. 力与速度的乘积(得到功率)
65    println!("\n6. 力与速度的乘积(得到功率):");
66    let force = Force::from_newton(10.0); // 10 N
67    let velocity = Velocity::from_m_per_sec(5.0); // 5 m/s
68    let power_from_fv: Power = force * velocity; // 50 W
69
70    println!("   力: {} N", force.as_newton());
71    println!("   速度: {} m/s", velocity.as_m_per_sec());
72    println!("   功率: {} W", power_from_fv.as_watt());
73    println!("   验证: 10 N × 5 m/s = 50 W ✓");
74
75    // 7. 速度与力的乘积(满足交换律)
76    println!("\n7. 速度与力的乘积(满足交换律):");
77    let power_from_vf: Power = velocity * force; // 同样是 50 W
78    println!("   速度 × 力 = {} W", power_from_vf.as_watt());
79    println!("   验证: 5 m/s × 10 N = 50 W ✓");
80
81    // 8. 不同单位之间的运算
82    println!("\n8. 不同单位之间的运算:");
83    let p_micro = Power::from_micro_watt(1000000.0); // 1 W
84    let p_kilo = Power::from_kilo_watt(0.001);       // 1 W
85    let p_result = p_micro + p_kilo; // 1 W + 1 W = 2 W
86    println!("   {} μW + {} kW = {} W",
87             p_micro.as_micro_watt(),
88             p_kilo.as_kilo_watt(),
89             p_result.as_watt());
90
91    // 9. 马力转换
92    println!("\n9. 马力转换:");
93    let hp = Power::from_horse_power(1.0); // 1 hp
94    println!("   1 hp = {} W", hp.as_watt());
95    
96    let watt = Power::from_watt(745.7); // 745.7 W
97    println!("   745.7 W = {} hp", watt.as_horse_power());
98
99    // 10. 零值检查
100    println!("\n10. 零值检查:");
101    let p_zero = Power::from_watt(0.0);
102    let p_nonzero = Power::from_watt(1.0);
103    println!("   p_zero.is_zero() = {}", p_zero.is_zero());
104    println!("   p_nonzero.is_zero() = {}", p_nonzero.is_zero());
105
106    // 11. 实际应用示例
107    println!("\n11. 实际应用示例:");
108    
109    // 电灯泡功率
110    let lightbulb = Power::from_watt(60.0); // 60 W灯泡
111    let usage_time = TimeDelta::seconds(3600); // 1小时
112    let energy_consumed: Energy = lightbulb * usage_time; // 216 kJ
113    println!("   60W灯泡使用1小时消耗: {} kJ", energy_consumed.as_kilo_joule());
114
115    // 汽车发动机功率
116    let engine = Power::from_horse_power(200.0); // 200 hp发动机
117    println!("   200 hp发动机 = {} kW", engine.as_kilo_watt());
118    
119    // 风力发电机
120    let wind_turbine = Power::from_mega_watt(2.0); // 2 MW风力发电机
121    println!("   2 MW风力发电机 = {} hp", wind_turbine.as_horse_power());
122
123    // 12. 功率与时间的复杂计算
124    println!("\n12. 功率与时间的复杂计算:");
125    
126    // 不同功率设备的使用
127    let tv = Power::from_watt(100.0); // 100 W电视
128    let tv_time = TimeDelta::seconds(7200); // 2小时
129    
130    let computer = Power::from_watt(300.0); // 300 W电脑
131    let computer_time = TimeDelta::seconds(3600); // 1小时
132    
133    let total_energy: Energy = tv * tv_time + computer * computer_time;
134    println!("   电视2小时 + 电脑1小时 = {} kWh", total_energy.as_kilo_joule() / 3600.0);
135
136    // 13. 机械功率计算
137    println!("\n13. 机械功率计算:");
138    
139    // 电梯功率
140    let elevator_force = Force::from_kilo_newton(10.0); // 10 kN
141    let elevator_speed = Velocity::from_m_per_sec(2.0); // 2 m/s
142    let elevator_power: Power = elevator_force * elevator_speed; // 20 kW
143    println!("   电梯功率: {} kN × {} m/s = {} kW", 
144             elevator_force.as_kilo_newton(), 
145             elevator_speed.as_m_per_sec(), 
146             elevator_power.as_kilo_watt());
147
148    // 汽车牵引功率
149    let car_force = Force::from_newton(2000.0); // 2000 N牵引力
150    let car_speed = Velocity::from_km_per_h(80.0); // 80 km/h
151    let car_power: Power = car_force * car_speed; // 约44.4 kW
152    println!("   汽车牵引功率: {} N × {} km/h = {} kW", 
153             car_force.as_newton(), 
154             car_speed.as_km_per_h(), 
155             car_power.as_kilo_watt());
156
157    println!("\n=== 示例完成 ===");
158}
Source

pub fn from_light_speed(v: f64) -> Self

Source

pub fn from_km_per_h(v: f64) -> Self

Examples found in repository?
examples/example_new_operations.rs (line 68)
4fn main() {
5    println!("=== 新增物理量运算关系示例 ===\n");
6
7    // 1. 能量 ÷ 时间 = 功率
8    println!("1. 能量 ÷ 时间 = 功率");
9    let energy = Energy::from_joule(1000.0); // 1000 J
10    let time = TimeDelta::seconds(10); // 10 s
11    let power: Power = energy / time; // 100 W
12    println!("   1000 J ÷ 10 s = {} W", power.as_watt());
13    println!("   验证: {} W × 10 s = {} J\n", power.as_watt(), (power * time).as_joule());
14
15    // 2. 力矩 ÷ 距离 = 力
16    println!("2. 力矩 ÷ 距离 = 力");
17    let torque = Torque::from_nm(50.0); // 50 N·m
18    let distance = Distance::from_m(5.0); // 5 m
19    let force: Force = torque / distance; // 10 N
20    println!("   50 N·m ÷ 5 m = {} N", force.as_newton());
21    println!("   验证: {} N × 5 m = {} J\n", force.as_newton(), (force * distance).as_joule());
22
23    // 3. 速度 ÷ 加速度 = 时间
24    println!("3. 速度 ÷ 加速度 = 时间");
25    let velocity = Velocity::from_m_per_sec(20.0); // 20 m/s
26    let acceleration = Acceleration::from_m_per_s2(4.0); // 4 m/s²
27    let time = velocity / acceleration; // 5 s
28    println!("   20 m/s ÷ 4 m/s² = {} s", time_delta_to_secs_f64(&time));
29    println!("   验证: 4 m/s² × {} s = {} m/s\n", time_delta_to_secs_f64(&time), (acceleration * time).as_m_per_sec());
30
31    // 4. 距离 ÷ 速度 = 时间
32    println!("4. 距离 ÷ 速度 = 时间");
33    let distance = Distance::from_m(200.0); // 200 m
34    let velocity = Velocity::from_m_per_sec(25.0); // 25 m/s
35    let time = distance / velocity; // 8 s
36    println!("   200 m ÷ 25 m/s = {} s", time_delta_to_secs_f64(&time));
37    println!("   验证: 25 m/s × {} s = {} m\n", time_delta_to_secs_f64(&time), (velocity * time).as_m());
38
39    // 5. 功率 ÷ 力 = 速度
40    println!("5. 功率 ÷ 力 = 速度");
41    let power = Power::from_watt(300.0); // 300 W
42    let force = Force::from_newton(15.0); // 15 N
43    let velocity: Velocity = power / force; // 20 m/s
44    println!("   300 W ÷ 15 N = {} m/s", velocity.as_m_per_sec());
45    println!("   验证: 15 N × {} m/s = {} W\n", velocity.as_m_per_sec(), (force * velocity).as_watt());
46
47    // 6. 功率 ÷ 速度 = 力
48    println!("6. 功率 ÷ 速度 = 力");
49    let power = Power::from_watt(400.0); // 400 W
50    let velocity = Velocity::from_m_per_sec(16.0); // 16 m/s
51    let force: Force = power / velocity; // 25 N
52    println!("   400 W ÷ 16 m/s = {} N", force.as_newton());
53    println!("   验证: {} N × 16 m/s = {} W\n", force.as_newton(), (force * velocity).as_watt());
54
55    // 7. 角速度 ÷ 角加速度 = 时间
56    println!("7. 角速度 ÷ 角加速度 = 时间");
57    let omega = AngularVelocity::from_rad_per_second(12.0); // 12 rad/s
58    let alpha = AngularAcceleration::from_rad_per_second2(3.0); // 3 rad/s²
59    let time = omega / alpha; // 4 s
60    println!("   12 rad/s ÷ 3 rad/s² = {} s", time_delta_to_secs_f64(&time));
61    println!("   验证: 3 rad/s² × {} s = {} rad/s\n", time_delta_to_secs_f64(&time), (alpha * time).as_rad_per_second());
62
63    // 实际应用示例
64    println!("=== 实际应用示例 ===");
65    
66    // 汽车制动距离计算
67    println!("\n汽车制动距离计算:");
68    let initial_velocity = Velocity::from_km_per_h(60.0); // 60 km/h
69    let deceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
70    let braking_time = initial_velocity / deceleration; // 制动时间
71    let braking_distance = initial_velocity * braking_time; // 制动距离
72    println!("   初始速度: {} km/h", initial_velocity.as_km_per_h());
73    println!("   减速度: {} m/s²", deceleration.as_m_per_s2());
74    println!("   制动时间: {:.2} s", time_delta_to_secs_f64(&braking_time));
75    println!("   制动距离: {:.2} m", braking_distance.as_m());
76
77    // 电机功率计算
78    println!("\n电机功率计算:");
79    let torque = Torque::from_nm(100.0); // 100 N·m
80    let angular_velocity = AngularVelocity::from_rad_per_second(50.0); // 50 rad/s
81    let power: Power = torque * angular_velocity; // 5000 W
82    println!("   扭矩: {} N·m", torque.as_nm());
83    println!("   角速度: {} rad/s", angular_velocity.as_rad_per_second());
84    println!("   功率: {:.1} W ({:.2} kW)", power.as_watt(), power.as_kilo_watt());
85
86    // 杠杆原理验证
87    println!("\n杠杆原理验证:");
88    let force = Force::from_newton(20.0); // 20 N
89    let lever_arm = Distance::from_m(3.0); // 3 m
90    let energy = force * lever_arm; // 60 J
91    let calculated_force: Force = energy / lever_arm; // 20 N
92    println!("   力: {} N", force.as_newton());
93    println!("   力臂: {} m", lever_arm.as_m());
94    println!("   功: {} J", energy.as_joule());
95    println!("   验证力: {} N", calculated_force.as_newton());
96
97    println!("\n=== 示例完成 ===");
98}
More examples
Hide additional examples
examples/example_power.rs (line 150)
5fn main() {
6    println!("=== 功率物理量示例 ===\n");
7
8    // 1. 创建不同单位的功率
9    println!("1. 创建不同单位的功率:");
10    let p1 = Power::from_watt(1.0); // 1 W
11    let p2 = Power::from_mill_watt(1000.0); // 1000 mW = 1 W
12    let p3 = Power::from_micro_watt(1000000.0); // 1000000 μW = 1 W
13    let p4 = Power::from_nano_watt(1000000000.0); // 1000000000 nW = 1 W
14    let p5 = Power::from_kilo_watt(0.001); // 0.001 kW = 1 W
15    let p6 = Power::from_mega_watt(0.000001); // 0.000001 MW = 1 W
16    let p7 = Power::from_horse_power(1.0 / 745.7); // 1/745.7 hp ≈ 1 W
17
18    println!("   p1 = {} W", p1.as_watt());
19    println!("   p2 = {} mW = {} W", p2.as_mill_watt(), p2.as_watt());
20    println!("   p3 = {} μW = {} W", p3.as_micro_watt(), p3.as_watt());
21    println!("   p4 = {} nW = {} W", p4.as_nano_watt(), p4.as_watt());
22    println!("   p5 = {} kW = {} W", p5.as_kilo_watt(), p5.as_watt());
23    println!("   p6 = {} MW = {} W", p6.as_mega_watt(), p6.as_watt());
24    println!("   p7 = {} hp = {} W", p7.as_horse_power(), p7.as_watt());
25
26    // 2. 单位转换
27    println!("\n2. 单位转换:");
28    println!("   1 W = {} mW", p1.as_mill_watt());
29    println!("   1 W = {} μW", p1.as_micro_watt());
30    println!("   1 W = {} nW", p1.as_nano_watt());
31    println!("   1 W = {} kW", p1.as_kilo_watt());
32    println!("   1 W = {} MW", p1.as_mega_watt());
33    println!("   1 W = {} hp", p1.as_horse_power());
34
35    // 3. 功率的运算
36    println!("\n3. 功率的运算:");
37    let p_sum = p1 + p2; // 1 W + 1 W = 2 W
38    let p_diff = p_sum - p1; // 2 W - 1 W = 1 W
39    let p_scale = p1 * 3.0; // 1 W * 3 = 3 W
40    let p_div = p_scale / 2.0; // 3 W / 2 = 1.5 W
41
42    println!("   p1 + p2 = {} W", p_sum.as_watt());
43    println!("   p_sum - p1 = {} W", p_diff.as_watt());
44    println!("   p1 * 3 = {} W", p_scale.as_watt());
45    println!("   p_scale / 2 = {} W", p_div.as_watt());
46
47    // 4. 功率与时间的乘积(得到能量)
48    println!("\n4. 功率与时间的乘积(得到能量):");
49    let power = Power::from_watt(100.0); // 100 W
50    let time = TimeDelta::seconds(5); // 5 s
51    let energy: Energy = power * time; // 500 J
52
53    println!("   功率: {} W", power.as_watt());
54    println!("   时间: {} s", time_delta_to_secs_f64(&time));
55    println!("   能量: {} J", energy.as_joule());
56    println!("   验证: 100 W × 5 s = 500 J ✓");
57
58    // 5. 时间与功率的乘积(满足交换律)
59    println!("\n5. 时间与功率的乘积(满足交换律):");
60    let energy2: Energy = time * power; // 同样是 500 J
61    println!("   时间 × 功率 = {} J", energy2.as_joule());
62    println!("   验证: 5 s × 100 W = 500 J ✓");
63
64    // 6. 力与速度的乘积(得到功率)
65    println!("\n6. 力与速度的乘积(得到功率):");
66    let force = Force::from_newton(10.0); // 10 N
67    let velocity = Velocity::from_m_per_sec(5.0); // 5 m/s
68    let power_from_fv: Power = force * velocity; // 50 W
69
70    println!("   力: {} N", force.as_newton());
71    println!("   速度: {} m/s", velocity.as_m_per_sec());
72    println!("   功率: {} W", power_from_fv.as_watt());
73    println!("   验证: 10 N × 5 m/s = 50 W ✓");
74
75    // 7. 速度与力的乘积(满足交换律)
76    println!("\n7. 速度与力的乘积(满足交换律):");
77    let power_from_vf: Power = velocity * force; // 同样是 50 W
78    println!("   速度 × 力 = {} W", power_from_vf.as_watt());
79    println!("   验证: 5 m/s × 10 N = 50 W ✓");
80
81    // 8. 不同单位之间的运算
82    println!("\n8. 不同单位之间的运算:");
83    let p_micro = Power::from_micro_watt(1000000.0); // 1 W
84    let p_kilo = Power::from_kilo_watt(0.001);       // 1 W
85    let p_result = p_micro + p_kilo; // 1 W + 1 W = 2 W
86    println!("   {} μW + {} kW = {} W",
87             p_micro.as_micro_watt(),
88             p_kilo.as_kilo_watt(),
89             p_result.as_watt());
90
91    // 9. 马力转换
92    println!("\n9. 马力转换:");
93    let hp = Power::from_horse_power(1.0); // 1 hp
94    println!("   1 hp = {} W", hp.as_watt());
95    
96    let watt = Power::from_watt(745.7); // 745.7 W
97    println!("   745.7 W = {} hp", watt.as_horse_power());
98
99    // 10. 零值检查
100    println!("\n10. 零值检查:");
101    let p_zero = Power::from_watt(0.0);
102    let p_nonzero = Power::from_watt(1.0);
103    println!("   p_zero.is_zero() = {}", p_zero.is_zero());
104    println!("   p_nonzero.is_zero() = {}", p_nonzero.is_zero());
105
106    // 11. 实际应用示例
107    println!("\n11. 实际应用示例:");
108    
109    // 电灯泡功率
110    let lightbulb = Power::from_watt(60.0); // 60 W灯泡
111    let usage_time = TimeDelta::seconds(3600); // 1小时
112    let energy_consumed: Energy = lightbulb * usage_time; // 216 kJ
113    println!("   60W灯泡使用1小时消耗: {} kJ", energy_consumed.as_kilo_joule());
114
115    // 汽车发动机功率
116    let engine = Power::from_horse_power(200.0); // 200 hp发动机
117    println!("   200 hp发动机 = {} kW", engine.as_kilo_watt());
118    
119    // 风力发电机
120    let wind_turbine = Power::from_mega_watt(2.0); // 2 MW风力发电机
121    println!("   2 MW风力发电机 = {} hp", wind_turbine.as_horse_power());
122
123    // 12. 功率与时间的复杂计算
124    println!("\n12. 功率与时间的复杂计算:");
125    
126    // 不同功率设备的使用
127    let tv = Power::from_watt(100.0); // 100 W电视
128    let tv_time = TimeDelta::seconds(7200); // 2小时
129    
130    let computer = Power::from_watt(300.0); // 300 W电脑
131    let computer_time = TimeDelta::seconds(3600); // 1小时
132    
133    let total_energy: Energy = tv * tv_time + computer * computer_time;
134    println!("   电视2小时 + 电脑1小时 = {} kWh", total_energy.as_kilo_joule() / 3600.0);
135
136    // 13. 机械功率计算
137    println!("\n13. 机械功率计算:");
138    
139    // 电梯功率
140    let elevator_force = Force::from_kilo_newton(10.0); // 10 kN
141    let elevator_speed = Velocity::from_m_per_sec(2.0); // 2 m/s
142    let elevator_power: Power = elevator_force * elevator_speed; // 20 kW
143    println!("   电梯功率: {} kN × {} m/s = {} kW", 
144             elevator_force.as_kilo_newton(), 
145             elevator_speed.as_m_per_sec(), 
146             elevator_power.as_kilo_watt());
147
148    // 汽车牵引功率
149    let car_force = Force::from_newton(2000.0); // 2000 N牵引力
150    let car_speed = Velocity::from_km_per_h(80.0); // 80 km/h
151    let car_power: Power = car_force * car_speed; // 约44.4 kW
152    println!("   汽车牵引功率: {} N × {} km/h = {} kW", 
153             car_force.as_newton(), 
154             car_speed.as_km_per_h(), 
155             car_power.as_kilo_watt());
156
157    println!("\n=== 示例完成 ===");
158}
Source

pub fn from_km_per_sec(v: f64) -> Self

Source

pub fn as_m_per_sec(&self) -> f64

Examples found in repository?
examples/example_physics.rs (line 24)
19fn calculate() {
20    let distance = Distance::from_m(1000.0);
21    let time = TimeDelta::seconds(10);
22    let velocity: Velocity = distance / time;
23    let acc: Acceleration = velocity / time;
24    println!("速度: {} m/s, 加速度: {} m/s²", velocity.as_m_per_sec(), acc.as_m_per_s2());
25
26    let ang: Angular = Angular::from_deg(180.0);
27    let time = TimeDelta::seconds(10);
28    let omg: AngularVelocity = ang / time;
29    let angular_accel: AngularAcceleration = omg / time;
30    println!(
31        "角速度: {} rad/s, 角加速度: {} rad/s²",
32        omg.as_rad_per_second(),
33        angular_accel.as_rad_per_second2()
34    );
35
36    let dis: Distance = Distance::from_m(1000.0);
37    let area: Area = dis * dis;
38    println!("面积: {} m²", area.as_m2());
39}
More examples
Hide additional examples
examples/example_vector_quantity.rs (line 17)
3fn main() {
4    println!("=== VectorQuantity Trait 验证示例 ===\n");
5
6    // 验证支持向量的物理量
7    println!("1. 验证支持向量的物理量:");
8    
9    // 创建一些支持向量的物理量
10    let distance = Distance::from_m(10.0);
11    let velocity = Velocity::from_m_per_sec(5.0);
12    let force = Force::from_newton(20.0);
13    let torque = Torque::from_nm(15.0);
14    let magnetic_induction = MagneticInduction::from_tesla(0.5);
15
16    println!("   Distance: {} m", distance.as_m());
17    println!("   Velocity: {} m/s", velocity.as_m_per_sec());
18    println!("   Force: {} N", force.as_newton());
19    println!("   Torque: {} N·m", torque.as_nm());
20    println!("   MagneticInduction: {} T", magnetic_induction.as_tesla());
21
22    // 验证这些类型都实现了 VectorQuantity trait
23    println!("\n2. 验证 VectorQuantity trait 实现:");
24    
25    // 这些函数调用会验证类型是否实现了 VectorQuantity trait
26    check_vector_quantity(distance);
27    check_vector_quantity(velocity);
28    check_vector_quantity(force);
29    check_vector_quantity(torque);
30    check_vector_quantity(magnetic_induction);
31
32    println!("   ✓ 所有物理量都正确实现了 VectorQuantity trait");
33
34    // 创建 Vector3 实例来验证向量操作
35    println!("\n3. 验证 Vector3 操作:");
36    
37    let distance_vector = Vector3::new(
38        Distance::from_m(1.0),
39        Distance::from_m(2.0),
40        Distance::from_m(3.0)
41    );
42    
43    let velocity_vector = Vector3::new(
44        Velocity::from_m_per_sec(4.0),
45        Velocity::from_m_per_sec(5.0),
46        Velocity::from_m_per_sec(6.0)
47    );
48
49    println!("   Distance Vector: ({}, {}, {}) m", 
50             distance_vector.x.as_m(), 
51             distance_vector.y.as_m(), 
52             distance_vector.z.as_m());
53    
54    println!("   Velocity Vector: ({}, {}, {}) m/s", 
55             velocity_vector.x.as_m_per_sec(), 
56             velocity_vector.y.as_m_per_sec(), 
57             velocity_vector.z.as_m_per_sec());
58
59    // 验证向量运算 - 只能对相同类型的向量进行运算
60    let sum_distance = distance_vector + distance_vector;
61    println!("   Sum Distance Vector: ({}, {}, {}) m", 
62             sum_distance.x.as_m(), 
63             sum_distance.y.as_m(), 
64             sum_distance.z.as_m());
65
66    println!("\n=== 验证完成 ===");
67}
examples/example_vector_quantity_restriction.rs (line 25)
3fn main() {
4    println!("=== Vector3 类型约束验证示例 ===\n");
5
6    // 1. 验证支持向量的物理量可以用于 Vector3
7    println!("1. 支持向量的物理量可以用于 Vector3:");
8    
9    let distance_vector = Vector3::new(
10        Distance::from_m(1.0),
11        Distance::from_m(2.0),
12        Distance::from_m(3.0)
13    );
14    println!("   ✓ Distance Vector: ({}, {}, {}) m", 
15             distance_vector.x.as_m(), 
16             distance_vector.y.as_m(), 
17             distance_vector.z.as_m());
18
19    let velocity_vector = Vector3::new(
20        Velocity::from_m_per_sec(4.0),
21        Velocity::from_m_per_sec(5.0),
22        Velocity::from_m_per_sec(6.0)
23    );
24    println!("   ✓ Velocity Vector: ({}, {}, {}) m/s", 
25             velocity_vector.x.as_m_per_sec(), 
26             velocity_vector.y.as_m_per_sec(), 
27             velocity_vector.z.as_m_per_sec());
28
29    let force_vector = Vector3::new(
30        Force::from_newton(10.0),
31        Force::from_newton(20.0),
32        Force::from_newton(30.0)
33    );
34    println!("   ✓ Force Vector: ({}, {}, {}) N", 
35             force_vector.x.as_newton(), 
36             force_vector.y.as_newton(), 
37             force_vector.z.as_newton());
38
39    // 2. 验证不支持向量的物理量无法用于 Vector3
40    println!("\n2. 不支持向量的物理量无法用于 Vector3:");
41    println!("   ✓ 以下代码会在编译时被拒绝:");
42    println!("     let mass_vector = Vector3::new(");
43    println!("         Mass::from_kg(1.0),");
44    println!("         Mass::from_kg(2.0),");
45    println!("         Mass::from_kg(3.0)");
46    println!("     );");
47    println!("\n   ✓ 以下代码会在编译时被拒绝:");
48    println!("     let energy_vector = Vector3::new(");
49    println!("         Energy::from_joule(1.0),");
50    println!("         Energy::from_joule(2.0),");
51    println!("         Energy::from_joule(3.0)");
52    println!("     );");
53
54    // 3. 列出所有支持向量的物理量
55    println!("\n3. 支持向量的物理量列表:");
56    println!("   ✓ Distance (距离/位移)");
57    println!("   ✓ Velocity (速度)");
58    println!("   ✓ Acceleration (加速度)");
59    println!("   ✓ Angular (角度)");
60    println!("   ✓ AngularVelocity (角速度)");
61    println!("   ✓ AngularAcceleration (角加速度)");
62    println!("   ✓ Coef (系数)");
63    println!("   ✓ AngularMomentum (角动量)");
64    println!("   ✓ Momentum (动量)");
65    println!("   ✓ MagneticInduction (磁感应强度)");
66    println!("   ✓ MagneticMoment (磁矩)");
67    println!("   ✓ Torque (力矩)");
68    println!("   ✓ Force (力)");
69    println!("   ✓ MagneticAngularVelocity (磁角速度)");
70
71    // 4. 列出不支持向量的物理量
72    println!("\n4. 不支持向量的物理量列表:");
73    println!("   ✗ Mass (质量)");
74    println!("   ✗ Energy (能量)");
75    println!("   ✗ Power (功率)");
76    println!("   ✗ Volume (体积)");
77    println!("   ✗ Area (面积)");
78    println!("   ✗ Time (时间)");
79
80    println!("\n=== 验证完成 ===");
81    println!("Vector3 现在只能用于支持向量的物理量,这确保了类型安全!");
82}
examples/example_energy.rs (line 81)
3fn main() {
4    println!("=== 能量物理量示例 ===\n");
5
6    // 1. 创建不同单位的能量
7    println!("1. 创建不同单位的能量:");
8    let e1 = Energy::from_joule(1.0); // 1 J
9    let e2 = Energy::from_mill_joule(1000.0); // 1000 mJ = 1 J
10    let e3 = Energy::from_micro_joule(1e6); // 1e6 μJ = 1 J
11    let e4 = Energy::from_nano_joule(1e9); // 1e9 nJ = 1 J
12    let e5 = Energy::from_kilo_joule(1e-3); // 1e-3 kJ = 1 J
13    let e6 = Energy::from_mega_joule(1e-6); // 1e-6 MJ = 1 J
14    let e7 = Energy::from_electron_volt(1.0 / 1.602176634e-19); // 1 J
15    let e8 = Energy::from_kilo_electron_volt(1.0 / 1.602176634e-16); // 1 J
16    let e9 = Energy::from_mega_electron_volt(1.0 / 1.602176634e-13); // 1 J
17
18    println!("   e1 = {} J", e1.as_joule());
19    println!("   e2 = {} mJ = {} J", e2.as_mill_joule(), e2.as_joule());
20    println!("   e3 = {} μJ = {} J", e3.as_micro_joule(), e3.as_joule());
21    println!("   e4 = {} nJ = {} J", e4.as_nano_joule(), e4.as_joule());
22    println!("   e5 = {} kJ = {} J", e5.as_kilo_joule(), e5.as_joule());
23    println!("   e6 = {} MJ = {} J", e6.as_mega_joule(), e6.as_joule());
24    println!("   e7 = {} eV = {} J", e7.as_electron_volt(), e7.as_joule());
25    println!("   e8 = {} keV = {} J", e8.as_kilo_electron_volt(), e8.as_joule());
26    println!("   e9 = {} MeV = {} J", e9.as_mega_electron_volt(), e9.as_joule());
27
28    // 2. 单位转换
29    println!("\n2. 单位转换:");
30    println!("   1 J = {} mJ", e1.as_mill_joule());
31    println!("   1 J = {} μJ", e1.as_micro_joule());
32    println!("   1 J = {} nJ", e1.as_nano_joule());
33    println!("   1 J = {} kJ", e1.as_kilo_joule());
34    println!("   1 J = {} MJ", e1.as_mega_joule());
35    println!("   1 J = {} eV", e1.as_electron_volt());
36    println!("   1 J = {} keV", e1.as_kilo_electron_volt());
37    println!("   1 J = {} MeV", e1.as_mega_electron_volt());
38
39    // 3. 能量运算
40    println!("\n3. 能量运算:");
41    let e_sum = e1 + e2; // 1 J + 1 J = 2 J
42    let e_diff = e_sum - e1; // 2 J - 1 J = 1 J
43    let e_scale = e1 * 3.0; // 1 J * 3 = 3 J
44    let e_div = e_scale / 2.0; // 3 J / 2 = 1.5 J
45
46    println!("   e1 + e2 = {} J", e_sum.as_joule());
47    println!("   e_sum - e1 = {} J", e_diff.as_joule());
48    println!("   e1 * 3 = {} J", e_scale.as_joule());
49    println!("   e_scale / 2 = {} J", e_div.as_joule());
50
51    // 4. 不同单位之间的运算
52    println!("\n4. 不同单位之间的运算:");
53    let e_micro = Energy::from_micro_joule(1e6); // 1 J
54    let e_mill = Energy::from_mill_joule(1000.0); // 1 J
55    let e_result = e_micro + e_mill; // 1 J + 1 J = 2 J
56    println!("   {} μJ + {} mJ = {} J", 
57             e_micro.as_micro_joule(), 
58             e_mill.as_mill_joule(), 
59             e_result.as_joule());
60
61    // 5. 标量与能量的运算
62    println!("\n5. 标量与能量的运算:");
63    let e_scaled = 5.0 * e1; // 5 × 1 J = 5 J
64    let e_divided = 10.0 / e1; // 10 / 1 J = 10 J
65    println!("   5.0 * {} J = {} J", e1.as_joule(), e_scaled.as_joule());
66    println!("   10.0 / {} J = {} J", e1.as_joule(), e_divided.as_joule());
67
68    // 6. 零值检查
69    println!("\n6. 零值检查:");
70    let e_zero = Energy::from_joule(0.0);
71    let e_nonzero = Energy::from_joule(1.0);
72    println!("   e_zero.is_zero() = {}", e_zero.is_zero());
73    println!("   e_nonzero.is_zero() = {}", e_nonzero.is_zero());
74
75    // 7. 实际应用示例
76    println!("\n7. 实际应用示例:");
77    
78    // 动能计算
79    let mass = Mass::from_kg(2.0); // 2 kg
80    let velocity = Velocity::from_m_per_sec(3.0); // 3 m/s
81    let kinetic_energy = Energy::from_joule(0.5 * mass.as_kg() * velocity.as_m_per_sec().powi(2));
82    println!("   质量: {} kg", mass.as_kg());
83    println!("   速度: {} m/s", velocity.as_m_per_sec());
84    println!("   动能: {} J", kinetic_energy.as_joule());
85
86    // 势能计算
87    let mass = Mass::from_kg(1.0); // 1 kg
88    let height = Distance::from_m(10.0); // 10 m
89    let g = 9.81; // 重力加速度 m/s²
90    let potential_energy = Energy::from_joule(mass.as_kg() * g * height.as_m());
91    println!("   质量: {} kg", mass.as_kg());
92    println!("   高度: {} m", height.as_m());
93    println!("   势能: {} J", potential_energy.as_joule());
94
95    // 电子伏特应用
96    let photon_energy = Energy::from_electron_volt(2.5); // 2.5 eV
97    println!("   光子能量: {} eV = {} J", 
98             photon_energy.as_electron_volt(), 
99             photon_energy.as_joule());
100
101    let nuclear_energy = Energy::from_mega_electron_volt(1.0); // 1 MeV
102    println!("   核能: {} MeV = {} J", 
103             nuclear_energy.as_mega_electron_volt(), 
104             nuclear_energy.as_joule());
105
106    println!("\n=== 示例完成 ===");
107}
examples/example_new_operations.rs (line 29)
4fn main() {
5    println!("=== 新增物理量运算关系示例 ===\n");
6
7    // 1. 能量 ÷ 时间 = 功率
8    println!("1. 能量 ÷ 时间 = 功率");
9    let energy = Energy::from_joule(1000.0); // 1000 J
10    let time = TimeDelta::seconds(10); // 10 s
11    let power: Power = energy / time; // 100 W
12    println!("   1000 J ÷ 10 s = {} W", power.as_watt());
13    println!("   验证: {} W × 10 s = {} J\n", power.as_watt(), (power * time).as_joule());
14
15    // 2. 力矩 ÷ 距离 = 力
16    println!("2. 力矩 ÷ 距离 = 力");
17    let torque = Torque::from_nm(50.0); // 50 N·m
18    let distance = Distance::from_m(5.0); // 5 m
19    let force: Force = torque / distance; // 10 N
20    println!("   50 N·m ÷ 5 m = {} N", force.as_newton());
21    println!("   验证: {} N × 5 m = {} J\n", force.as_newton(), (force * distance).as_joule());
22
23    // 3. 速度 ÷ 加速度 = 时间
24    println!("3. 速度 ÷ 加速度 = 时间");
25    let velocity = Velocity::from_m_per_sec(20.0); // 20 m/s
26    let acceleration = Acceleration::from_m_per_s2(4.0); // 4 m/s²
27    let time = velocity / acceleration; // 5 s
28    println!("   20 m/s ÷ 4 m/s² = {} s", time_delta_to_secs_f64(&time));
29    println!("   验证: 4 m/s² × {} s = {} m/s\n", time_delta_to_secs_f64(&time), (acceleration * time).as_m_per_sec());
30
31    // 4. 距离 ÷ 速度 = 时间
32    println!("4. 距离 ÷ 速度 = 时间");
33    let distance = Distance::from_m(200.0); // 200 m
34    let velocity = Velocity::from_m_per_sec(25.0); // 25 m/s
35    let time = distance / velocity; // 8 s
36    println!("   200 m ÷ 25 m/s = {} s", time_delta_to_secs_f64(&time));
37    println!("   验证: 25 m/s × {} s = {} m\n", time_delta_to_secs_f64(&time), (velocity * time).as_m());
38
39    // 5. 功率 ÷ 力 = 速度
40    println!("5. 功率 ÷ 力 = 速度");
41    let power = Power::from_watt(300.0); // 300 W
42    let force = Force::from_newton(15.0); // 15 N
43    let velocity: Velocity = power / force; // 20 m/s
44    println!("   300 W ÷ 15 N = {} m/s", velocity.as_m_per_sec());
45    println!("   验证: 15 N × {} m/s = {} W\n", velocity.as_m_per_sec(), (force * velocity).as_watt());
46
47    // 6. 功率 ÷ 速度 = 力
48    println!("6. 功率 ÷ 速度 = 力");
49    let power = Power::from_watt(400.0); // 400 W
50    let velocity = Velocity::from_m_per_sec(16.0); // 16 m/s
51    let force: Force = power / velocity; // 25 N
52    println!("   400 W ÷ 16 m/s = {} N", force.as_newton());
53    println!("   验证: {} N × 16 m/s = {} W\n", force.as_newton(), (force * velocity).as_watt());
54
55    // 7. 角速度 ÷ 角加速度 = 时间
56    println!("7. 角速度 ÷ 角加速度 = 时间");
57    let omega = AngularVelocity::from_rad_per_second(12.0); // 12 rad/s
58    let alpha = AngularAcceleration::from_rad_per_second2(3.0); // 3 rad/s²
59    let time = omega / alpha; // 4 s
60    println!("   12 rad/s ÷ 3 rad/s² = {} s", time_delta_to_secs_f64(&time));
61    println!("   验证: 3 rad/s² × {} s = {} rad/s\n", time_delta_to_secs_f64(&time), (alpha * time).as_rad_per_second());
62
63    // 实际应用示例
64    println!("=== 实际应用示例 ===");
65    
66    // 汽车制动距离计算
67    println!("\n汽车制动距离计算:");
68    let initial_velocity = Velocity::from_km_per_h(60.0); // 60 km/h
69    let deceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
70    let braking_time = initial_velocity / deceleration; // 制动时间
71    let braking_distance = initial_velocity * braking_time; // 制动距离
72    println!("   初始速度: {} km/h", initial_velocity.as_km_per_h());
73    println!("   减速度: {} m/s²", deceleration.as_m_per_s2());
74    println!("   制动时间: {:.2} s", time_delta_to_secs_f64(&braking_time));
75    println!("   制动距离: {:.2} m", braking_distance.as_m());
76
77    // 电机功率计算
78    println!("\n电机功率计算:");
79    let torque = Torque::from_nm(100.0); // 100 N·m
80    let angular_velocity = AngularVelocity::from_rad_per_second(50.0); // 50 rad/s
81    let power: Power = torque * angular_velocity; // 5000 W
82    println!("   扭矩: {} N·m", torque.as_nm());
83    println!("   角速度: {} rad/s", angular_velocity.as_rad_per_second());
84    println!("   功率: {:.1} W ({:.2} kW)", power.as_watt(), power.as_kilo_watt());
85
86    // 杠杆原理验证
87    println!("\n杠杆原理验证:");
88    let force = Force::from_newton(20.0); // 20 N
89    let lever_arm = Distance::from_m(3.0); // 3 m
90    let energy = force * lever_arm; // 60 J
91    let calculated_force: Force = energy / lever_arm; // 20 N
92    println!("   力: {} N", force.as_newton());
93    println!("   力臂: {} m", lever_arm.as_m());
94    println!("   功: {} J", energy.as_joule());
95    println!("   验证力: {} N", calculated_force.as_newton());
96
97    println!("\n=== 示例完成 ===");
98}
examples/example_power.rs (line 71)
5fn main() {
6    println!("=== 功率物理量示例 ===\n");
7
8    // 1. 创建不同单位的功率
9    println!("1. 创建不同单位的功率:");
10    let p1 = Power::from_watt(1.0); // 1 W
11    let p2 = Power::from_mill_watt(1000.0); // 1000 mW = 1 W
12    let p3 = Power::from_micro_watt(1000000.0); // 1000000 μW = 1 W
13    let p4 = Power::from_nano_watt(1000000000.0); // 1000000000 nW = 1 W
14    let p5 = Power::from_kilo_watt(0.001); // 0.001 kW = 1 W
15    let p6 = Power::from_mega_watt(0.000001); // 0.000001 MW = 1 W
16    let p7 = Power::from_horse_power(1.0 / 745.7); // 1/745.7 hp ≈ 1 W
17
18    println!("   p1 = {} W", p1.as_watt());
19    println!("   p2 = {} mW = {} W", p2.as_mill_watt(), p2.as_watt());
20    println!("   p3 = {} μW = {} W", p3.as_micro_watt(), p3.as_watt());
21    println!("   p4 = {} nW = {} W", p4.as_nano_watt(), p4.as_watt());
22    println!("   p5 = {} kW = {} W", p5.as_kilo_watt(), p5.as_watt());
23    println!("   p6 = {} MW = {} W", p6.as_mega_watt(), p6.as_watt());
24    println!("   p7 = {} hp = {} W", p7.as_horse_power(), p7.as_watt());
25
26    // 2. 单位转换
27    println!("\n2. 单位转换:");
28    println!("   1 W = {} mW", p1.as_mill_watt());
29    println!("   1 W = {} μW", p1.as_micro_watt());
30    println!("   1 W = {} nW", p1.as_nano_watt());
31    println!("   1 W = {} kW", p1.as_kilo_watt());
32    println!("   1 W = {} MW", p1.as_mega_watt());
33    println!("   1 W = {} hp", p1.as_horse_power());
34
35    // 3. 功率的运算
36    println!("\n3. 功率的运算:");
37    let p_sum = p1 + p2; // 1 W + 1 W = 2 W
38    let p_diff = p_sum - p1; // 2 W - 1 W = 1 W
39    let p_scale = p1 * 3.0; // 1 W * 3 = 3 W
40    let p_div = p_scale / 2.0; // 3 W / 2 = 1.5 W
41
42    println!("   p1 + p2 = {} W", p_sum.as_watt());
43    println!("   p_sum - p1 = {} W", p_diff.as_watt());
44    println!("   p1 * 3 = {} W", p_scale.as_watt());
45    println!("   p_scale / 2 = {} W", p_div.as_watt());
46
47    // 4. 功率与时间的乘积(得到能量)
48    println!("\n4. 功率与时间的乘积(得到能量):");
49    let power = Power::from_watt(100.0); // 100 W
50    let time = TimeDelta::seconds(5); // 5 s
51    let energy: Energy = power * time; // 500 J
52
53    println!("   功率: {} W", power.as_watt());
54    println!("   时间: {} s", time_delta_to_secs_f64(&time));
55    println!("   能量: {} J", energy.as_joule());
56    println!("   验证: 100 W × 5 s = 500 J ✓");
57
58    // 5. 时间与功率的乘积(满足交换律)
59    println!("\n5. 时间与功率的乘积(满足交换律):");
60    let energy2: Energy = time * power; // 同样是 500 J
61    println!("   时间 × 功率 = {} J", energy2.as_joule());
62    println!("   验证: 5 s × 100 W = 500 J ✓");
63
64    // 6. 力与速度的乘积(得到功率)
65    println!("\n6. 力与速度的乘积(得到功率):");
66    let force = Force::from_newton(10.0); // 10 N
67    let velocity = Velocity::from_m_per_sec(5.0); // 5 m/s
68    let power_from_fv: Power = force * velocity; // 50 W
69
70    println!("   力: {} N", force.as_newton());
71    println!("   速度: {} m/s", velocity.as_m_per_sec());
72    println!("   功率: {} W", power_from_fv.as_watt());
73    println!("   验证: 10 N × 5 m/s = 50 W ✓");
74
75    // 7. 速度与力的乘积(满足交换律)
76    println!("\n7. 速度与力的乘积(满足交换律):");
77    let power_from_vf: Power = velocity * force; // 同样是 50 W
78    println!("   速度 × 力 = {} W", power_from_vf.as_watt());
79    println!("   验证: 5 m/s × 10 N = 50 W ✓");
80
81    // 8. 不同单位之间的运算
82    println!("\n8. 不同单位之间的运算:");
83    let p_micro = Power::from_micro_watt(1000000.0); // 1 W
84    let p_kilo = Power::from_kilo_watt(0.001);       // 1 W
85    let p_result = p_micro + p_kilo; // 1 W + 1 W = 2 W
86    println!("   {} μW + {} kW = {} W",
87             p_micro.as_micro_watt(),
88             p_kilo.as_kilo_watt(),
89             p_result.as_watt());
90
91    // 9. 马力转换
92    println!("\n9. 马力转换:");
93    let hp = Power::from_horse_power(1.0); // 1 hp
94    println!("   1 hp = {} W", hp.as_watt());
95    
96    let watt = Power::from_watt(745.7); // 745.7 W
97    println!("   745.7 W = {} hp", watt.as_horse_power());
98
99    // 10. 零值检查
100    println!("\n10. 零值检查:");
101    let p_zero = Power::from_watt(0.0);
102    let p_nonzero = Power::from_watt(1.0);
103    println!("   p_zero.is_zero() = {}", p_zero.is_zero());
104    println!("   p_nonzero.is_zero() = {}", p_nonzero.is_zero());
105
106    // 11. 实际应用示例
107    println!("\n11. 实际应用示例:");
108    
109    // 电灯泡功率
110    let lightbulb = Power::from_watt(60.0); // 60 W灯泡
111    let usage_time = TimeDelta::seconds(3600); // 1小时
112    let energy_consumed: Energy = lightbulb * usage_time; // 216 kJ
113    println!("   60W灯泡使用1小时消耗: {} kJ", energy_consumed.as_kilo_joule());
114
115    // 汽车发动机功率
116    let engine = Power::from_horse_power(200.0); // 200 hp发动机
117    println!("   200 hp发动机 = {} kW", engine.as_kilo_watt());
118    
119    // 风力发电机
120    let wind_turbine = Power::from_mega_watt(2.0); // 2 MW风力发电机
121    println!("   2 MW风力发电机 = {} hp", wind_turbine.as_horse_power());
122
123    // 12. 功率与时间的复杂计算
124    println!("\n12. 功率与时间的复杂计算:");
125    
126    // 不同功率设备的使用
127    let tv = Power::from_watt(100.0); // 100 W电视
128    let tv_time = TimeDelta::seconds(7200); // 2小时
129    
130    let computer = Power::from_watt(300.0); // 300 W电脑
131    let computer_time = TimeDelta::seconds(3600); // 1小时
132    
133    let total_energy: Energy = tv * tv_time + computer * computer_time;
134    println!("   电视2小时 + 电脑1小时 = {} kWh", total_energy.as_kilo_joule() / 3600.0);
135
136    // 13. 机械功率计算
137    println!("\n13. 机械功率计算:");
138    
139    // 电梯功率
140    let elevator_force = Force::from_kilo_newton(10.0); // 10 kN
141    let elevator_speed = Velocity::from_m_per_sec(2.0); // 2 m/s
142    let elevator_power: Power = elevator_force * elevator_speed; // 20 kW
143    println!("   电梯功率: {} kN × {} m/s = {} kW", 
144             elevator_force.as_kilo_newton(), 
145             elevator_speed.as_m_per_sec(), 
146             elevator_power.as_kilo_watt());
147
148    // 汽车牵引功率
149    let car_force = Force::from_newton(2000.0); // 2000 N牵引力
150    let car_speed = Velocity::from_km_per_h(80.0); // 80 km/h
151    let car_power: Power = car_force * car_speed; // 约44.4 kW
152    println!("   汽车牵引功率: {} N × {} km/h = {} kW", 
153             car_force.as_newton(), 
154             car_speed.as_km_per_h(), 
155             car_power.as_kilo_watt());
156
157    println!("\n=== 示例完成 ===");
158}
Source

pub fn as_km_per_h(&self) -> f64

Examples found in repository?
examples/example_physics.rs (line 12)
4fn main(){
5    let distance = Distance::from_m(1001.0);
6    println!("{:?}", distance.as_km());
7    // 1.001 km
8    println!("{:?}", distance.as_light_year());
9    // 1.05805783485864e-13 light years(1001 m ÷ 9.4607e15 m/ly)
10
11    let velocity = Velocity::from_m_per_sec(100.0);
12    println!("{:?}", velocity.as_km_per_h());
13    // 360.0 km/h
14
15    calculate();
16    examples_vector3();
17}
More examples
Hide additional examples
examples/example_new_operations.rs (line 72)
4fn main() {
5    println!("=== 新增物理量运算关系示例 ===\n");
6
7    // 1. 能量 ÷ 时间 = 功率
8    println!("1. 能量 ÷ 时间 = 功率");
9    let energy = Energy::from_joule(1000.0); // 1000 J
10    let time = TimeDelta::seconds(10); // 10 s
11    let power: Power = energy / time; // 100 W
12    println!("   1000 J ÷ 10 s = {} W", power.as_watt());
13    println!("   验证: {} W × 10 s = {} J\n", power.as_watt(), (power * time).as_joule());
14
15    // 2. 力矩 ÷ 距离 = 力
16    println!("2. 力矩 ÷ 距离 = 力");
17    let torque = Torque::from_nm(50.0); // 50 N·m
18    let distance = Distance::from_m(5.0); // 5 m
19    let force: Force = torque / distance; // 10 N
20    println!("   50 N·m ÷ 5 m = {} N", force.as_newton());
21    println!("   验证: {} N × 5 m = {} J\n", force.as_newton(), (force * distance).as_joule());
22
23    // 3. 速度 ÷ 加速度 = 时间
24    println!("3. 速度 ÷ 加速度 = 时间");
25    let velocity = Velocity::from_m_per_sec(20.0); // 20 m/s
26    let acceleration = Acceleration::from_m_per_s2(4.0); // 4 m/s²
27    let time = velocity / acceleration; // 5 s
28    println!("   20 m/s ÷ 4 m/s² = {} s", time_delta_to_secs_f64(&time));
29    println!("   验证: 4 m/s² × {} s = {} m/s\n", time_delta_to_secs_f64(&time), (acceleration * time).as_m_per_sec());
30
31    // 4. 距离 ÷ 速度 = 时间
32    println!("4. 距离 ÷ 速度 = 时间");
33    let distance = Distance::from_m(200.0); // 200 m
34    let velocity = Velocity::from_m_per_sec(25.0); // 25 m/s
35    let time = distance / velocity; // 8 s
36    println!("   200 m ÷ 25 m/s = {} s", time_delta_to_secs_f64(&time));
37    println!("   验证: 25 m/s × {} s = {} m\n", time_delta_to_secs_f64(&time), (velocity * time).as_m());
38
39    // 5. 功率 ÷ 力 = 速度
40    println!("5. 功率 ÷ 力 = 速度");
41    let power = Power::from_watt(300.0); // 300 W
42    let force = Force::from_newton(15.0); // 15 N
43    let velocity: Velocity = power / force; // 20 m/s
44    println!("   300 W ÷ 15 N = {} m/s", velocity.as_m_per_sec());
45    println!("   验证: 15 N × {} m/s = {} W\n", velocity.as_m_per_sec(), (force * velocity).as_watt());
46
47    // 6. 功率 ÷ 速度 = 力
48    println!("6. 功率 ÷ 速度 = 力");
49    let power = Power::from_watt(400.0); // 400 W
50    let velocity = Velocity::from_m_per_sec(16.0); // 16 m/s
51    let force: Force = power / velocity; // 25 N
52    println!("   400 W ÷ 16 m/s = {} N", force.as_newton());
53    println!("   验证: {} N × 16 m/s = {} W\n", force.as_newton(), (force * velocity).as_watt());
54
55    // 7. 角速度 ÷ 角加速度 = 时间
56    println!("7. 角速度 ÷ 角加速度 = 时间");
57    let omega = AngularVelocity::from_rad_per_second(12.0); // 12 rad/s
58    let alpha = AngularAcceleration::from_rad_per_second2(3.0); // 3 rad/s²
59    let time = omega / alpha; // 4 s
60    println!("   12 rad/s ÷ 3 rad/s² = {} s", time_delta_to_secs_f64(&time));
61    println!("   验证: 3 rad/s² × {} s = {} rad/s\n", time_delta_to_secs_f64(&time), (alpha * time).as_rad_per_second());
62
63    // 实际应用示例
64    println!("=== 实际应用示例 ===");
65    
66    // 汽车制动距离计算
67    println!("\n汽车制动距离计算:");
68    let initial_velocity = Velocity::from_km_per_h(60.0); // 60 km/h
69    let deceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
70    let braking_time = initial_velocity / deceleration; // 制动时间
71    let braking_distance = initial_velocity * braking_time; // 制动距离
72    println!("   初始速度: {} km/h", initial_velocity.as_km_per_h());
73    println!("   减速度: {} m/s²", deceleration.as_m_per_s2());
74    println!("   制动时间: {:.2} s", time_delta_to_secs_f64(&braking_time));
75    println!("   制动距离: {:.2} m", braking_distance.as_m());
76
77    // 电机功率计算
78    println!("\n电机功率计算:");
79    let torque = Torque::from_nm(100.0); // 100 N·m
80    let angular_velocity = AngularVelocity::from_rad_per_second(50.0); // 50 rad/s
81    let power: Power = torque * angular_velocity; // 5000 W
82    println!("   扭矩: {} N·m", torque.as_nm());
83    println!("   角速度: {} rad/s", angular_velocity.as_rad_per_second());
84    println!("   功率: {:.1} W ({:.2} kW)", power.as_watt(), power.as_kilo_watt());
85
86    // 杠杆原理验证
87    println!("\n杠杆原理验证:");
88    let force = Force::from_newton(20.0); // 20 N
89    let lever_arm = Distance::from_m(3.0); // 3 m
90    let energy = force * lever_arm; // 60 J
91    let calculated_force: Force = energy / lever_arm; // 20 N
92    println!("   力: {} N", force.as_newton());
93    println!("   力臂: {} m", lever_arm.as_m());
94    println!("   功: {} J", energy.as_joule());
95    println!("   验证力: {} N", calculated_force.as_newton());
96
97    println!("\n=== 示例完成 ===");
98}
examples/example_power.rs (line 154)
5fn main() {
6    println!("=== 功率物理量示例 ===\n");
7
8    // 1. 创建不同单位的功率
9    println!("1. 创建不同单位的功率:");
10    let p1 = Power::from_watt(1.0); // 1 W
11    let p2 = Power::from_mill_watt(1000.0); // 1000 mW = 1 W
12    let p3 = Power::from_micro_watt(1000000.0); // 1000000 μW = 1 W
13    let p4 = Power::from_nano_watt(1000000000.0); // 1000000000 nW = 1 W
14    let p5 = Power::from_kilo_watt(0.001); // 0.001 kW = 1 W
15    let p6 = Power::from_mega_watt(0.000001); // 0.000001 MW = 1 W
16    let p7 = Power::from_horse_power(1.0 / 745.7); // 1/745.7 hp ≈ 1 W
17
18    println!("   p1 = {} W", p1.as_watt());
19    println!("   p2 = {} mW = {} W", p2.as_mill_watt(), p2.as_watt());
20    println!("   p3 = {} μW = {} W", p3.as_micro_watt(), p3.as_watt());
21    println!("   p4 = {} nW = {} W", p4.as_nano_watt(), p4.as_watt());
22    println!("   p5 = {} kW = {} W", p5.as_kilo_watt(), p5.as_watt());
23    println!("   p6 = {} MW = {} W", p6.as_mega_watt(), p6.as_watt());
24    println!("   p7 = {} hp = {} W", p7.as_horse_power(), p7.as_watt());
25
26    // 2. 单位转换
27    println!("\n2. 单位转换:");
28    println!("   1 W = {} mW", p1.as_mill_watt());
29    println!("   1 W = {} μW", p1.as_micro_watt());
30    println!("   1 W = {} nW", p1.as_nano_watt());
31    println!("   1 W = {} kW", p1.as_kilo_watt());
32    println!("   1 W = {} MW", p1.as_mega_watt());
33    println!("   1 W = {} hp", p1.as_horse_power());
34
35    // 3. 功率的运算
36    println!("\n3. 功率的运算:");
37    let p_sum = p1 + p2; // 1 W + 1 W = 2 W
38    let p_diff = p_sum - p1; // 2 W - 1 W = 1 W
39    let p_scale = p1 * 3.0; // 1 W * 3 = 3 W
40    let p_div = p_scale / 2.0; // 3 W / 2 = 1.5 W
41
42    println!("   p1 + p2 = {} W", p_sum.as_watt());
43    println!("   p_sum - p1 = {} W", p_diff.as_watt());
44    println!("   p1 * 3 = {} W", p_scale.as_watt());
45    println!("   p_scale / 2 = {} W", p_div.as_watt());
46
47    // 4. 功率与时间的乘积(得到能量)
48    println!("\n4. 功率与时间的乘积(得到能量):");
49    let power = Power::from_watt(100.0); // 100 W
50    let time = TimeDelta::seconds(5); // 5 s
51    let energy: Energy = power * time; // 500 J
52
53    println!("   功率: {} W", power.as_watt());
54    println!("   时间: {} s", time_delta_to_secs_f64(&time));
55    println!("   能量: {} J", energy.as_joule());
56    println!("   验证: 100 W × 5 s = 500 J ✓");
57
58    // 5. 时间与功率的乘积(满足交换律)
59    println!("\n5. 时间与功率的乘积(满足交换律):");
60    let energy2: Energy = time * power; // 同样是 500 J
61    println!("   时间 × 功率 = {} J", energy2.as_joule());
62    println!("   验证: 5 s × 100 W = 500 J ✓");
63
64    // 6. 力与速度的乘积(得到功率)
65    println!("\n6. 力与速度的乘积(得到功率):");
66    let force = Force::from_newton(10.0); // 10 N
67    let velocity = Velocity::from_m_per_sec(5.0); // 5 m/s
68    let power_from_fv: Power = force * velocity; // 50 W
69
70    println!("   力: {} N", force.as_newton());
71    println!("   速度: {} m/s", velocity.as_m_per_sec());
72    println!("   功率: {} W", power_from_fv.as_watt());
73    println!("   验证: 10 N × 5 m/s = 50 W ✓");
74
75    // 7. 速度与力的乘积(满足交换律)
76    println!("\n7. 速度与力的乘积(满足交换律):");
77    let power_from_vf: Power = velocity * force; // 同样是 50 W
78    println!("   速度 × 力 = {} W", power_from_vf.as_watt());
79    println!("   验证: 5 m/s × 10 N = 50 W ✓");
80
81    // 8. 不同单位之间的运算
82    println!("\n8. 不同单位之间的运算:");
83    let p_micro = Power::from_micro_watt(1000000.0); // 1 W
84    let p_kilo = Power::from_kilo_watt(0.001);       // 1 W
85    let p_result = p_micro + p_kilo; // 1 W + 1 W = 2 W
86    println!("   {} μW + {} kW = {} W",
87             p_micro.as_micro_watt(),
88             p_kilo.as_kilo_watt(),
89             p_result.as_watt());
90
91    // 9. 马力转换
92    println!("\n9. 马力转换:");
93    let hp = Power::from_horse_power(1.0); // 1 hp
94    println!("   1 hp = {} W", hp.as_watt());
95    
96    let watt = Power::from_watt(745.7); // 745.7 W
97    println!("   745.7 W = {} hp", watt.as_horse_power());
98
99    // 10. 零值检查
100    println!("\n10. 零值检查:");
101    let p_zero = Power::from_watt(0.0);
102    let p_nonzero = Power::from_watt(1.0);
103    println!("   p_zero.is_zero() = {}", p_zero.is_zero());
104    println!("   p_nonzero.is_zero() = {}", p_nonzero.is_zero());
105
106    // 11. 实际应用示例
107    println!("\n11. 实际应用示例:");
108    
109    // 电灯泡功率
110    let lightbulb = Power::from_watt(60.0); // 60 W灯泡
111    let usage_time = TimeDelta::seconds(3600); // 1小时
112    let energy_consumed: Energy = lightbulb * usage_time; // 216 kJ
113    println!("   60W灯泡使用1小时消耗: {} kJ", energy_consumed.as_kilo_joule());
114
115    // 汽车发动机功率
116    let engine = Power::from_horse_power(200.0); // 200 hp发动机
117    println!("   200 hp发动机 = {} kW", engine.as_kilo_watt());
118    
119    // 风力发电机
120    let wind_turbine = Power::from_mega_watt(2.0); // 2 MW风力发电机
121    println!("   2 MW风力发电机 = {} hp", wind_turbine.as_horse_power());
122
123    // 12. 功率与时间的复杂计算
124    println!("\n12. 功率与时间的复杂计算:");
125    
126    // 不同功率设备的使用
127    let tv = Power::from_watt(100.0); // 100 W电视
128    let tv_time = TimeDelta::seconds(7200); // 2小时
129    
130    let computer = Power::from_watt(300.0); // 300 W电脑
131    let computer_time = TimeDelta::seconds(3600); // 1小时
132    
133    let total_energy: Energy = tv * tv_time + computer * computer_time;
134    println!("   电视2小时 + 电脑1小时 = {} kWh", total_energy.as_kilo_joule() / 3600.0);
135
136    // 13. 机械功率计算
137    println!("\n13. 机械功率计算:");
138    
139    // 电梯功率
140    let elevator_force = Force::from_kilo_newton(10.0); // 10 kN
141    let elevator_speed = Velocity::from_m_per_sec(2.0); // 2 m/s
142    let elevator_power: Power = elevator_force * elevator_speed; // 20 kW
143    println!("   电梯功率: {} kN × {} m/s = {} kW", 
144             elevator_force.as_kilo_newton(), 
145             elevator_speed.as_m_per_sec(), 
146             elevator_power.as_kilo_watt());
147
148    // 汽车牵引功率
149    let car_force = Force::from_newton(2000.0); // 2000 N牵引力
150    let car_speed = Velocity::from_km_per_h(80.0); // 80 km/h
151    let car_power: Power = car_force * car_speed; // 约44.4 kW
152    println!("   汽车牵引功率: {} N × {} km/h = {} kW", 
153             car_force.as_newton(), 
154             car_speed.as_km_per_h(), 
155             car_power.as_kilo_watt());
156
157    println!("\n=== 示例完成 ===");
158}
Source

pub fn as_km_per_sec(&self) -> f64

Source

pub fn as_light_speed(&self) -> f64

Trait Implementations§

Source§

impl Add for Velocity

Source§

type Output = Velocity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<&'a Velocity> for Velocity

Source§

type Output = Velocity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'a Velocity) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a, 'b> Add<&'b Velocity> for &'a Velocity

Source§

type Output = Velocity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b Velocity) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<Velocity> for &'a Velocity

Source§

type Output = Velocity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Velocity) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<f64> for Velocity

Source§

type Output = Velocity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f64) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Velocity

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Velocity

Source§

impl Debug for Velocity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Velocity

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Velocity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Div<&'a Acceleration> for Velocity

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'a Acceleration) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<&'a Distance> for Velocity

Source§

type Output = AngularVelocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'a Distance) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<&'a Velocity> for Power

Source§

type Output = Force

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'a Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a, 'b> Div<&'b Acceleration> for &'a Velocity

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b Acceleration) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a, 'b> Div<&'b Distance> for &'a Velocity

Source§

type Output = AngularVelocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b Distance) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a, 'b> Div<&'b Velocity> for &'a Power

Source§

type Output = Force

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Acceleration> for Velocity

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Acceleration) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<Acceleration> for &'a Velocity

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Acceleration) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Coef> for Velocity

Source§

type Output = Velocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Coef) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Distance> for Velocity

Source§

type Output = AngularVelocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Distance) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<Distance> for &'a Velocity

Source§

type Output = AngularVelocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Distance) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<TimeDelta> for Velocity

Source§

type Output = Acceleration

The resulting type after applying the / operator.
Source§

fn div(self, duration: TimeDelta) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Velocity> for Distance

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Velocity> for Power

Source§

type Output = Force

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Div<Velocity> for &'a Power

Source§

type Output = Force

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Velocity> for f64

Source§

type Output = Velocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Velocity) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f64> for Velocity

Source§

type Output = Velocity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> Mul<&'a Mass> for Velocity

Source§

type Output = Momentum

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'a Mass) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a TimeDelta> for Velocity

Source§

type Output = Distance

The resulting type after applying the * operator.
Source§

fn mul(self, duration: &'a TimeDelta) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a, 'b> Mul<&'b Mass> for &'a Velocity

Source§

type Output = Momentum

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'b Mass) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a, 'b> Mul<&'b TimeDelta> for &'a Velocity

Source§

type Output = Distance

The resulting type after applying the * operator.
Source§

fn mul(self, duration: &'b TimeDelta) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Coef> for Velocity

Source§

type Output = Velocity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Coef) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Force> for Velocity

Source§

type Output = Power

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Force) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Mass> for Velocity

Source§

type Output = Momentum

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mass) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<Mass> for &'a Velocity

Source§

type Output = Momentum

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mass) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<TimeDelta> for Velocity

Source§

type Output = Distance

The resulting type after applying the * operator.
Source§

fn mul(self, duration: TimeDelta) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<TimeDelta> for &'a Velocity

Source§

type Output = Distance

The resulting type after applying the * operator.
Source§

fn mul(self, duration: TimeDelta) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Velocity> for Coef

Source§

type Output = Velocity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Velocity) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Velocity> for Mass

Source§

type Output = Momentum

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Velocity) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Velocity> for Force

Source§

type Output = Power

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Velocity) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Velocity> for f64

Source§

type Output = Velocity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Velocity) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f64> for Velocity

Source§

type Output = Velocity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl NonCoefficient for Velocity

Source§

impl PartialEq for Velocity

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PhysicalQuantity for Velocity

Source§

fn as_any(&self) -> &dyn Any

Source§

fn is_zero(&self) -> bool

Source§

fn default_unit_value(&self) -> f64

Source§

fn set_value(&mut self, value: f64)

Source§

fn unit_display_name(&self) -> String

返回物理量名称和单位,如 “Distance (m)”, “Velocity (m/s)”
Source§

impl StructuralPartialEq for Velocity

Source§

impl Sub for Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a> Sub<&'a Velocity> for Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'a Velocity) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a, 'b> Sub<&'b Velocity> for &'a Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b Velocity) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a> Sub<Velocity> for &'a Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Velocity) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<f64> for Velocity

Source§

type Output = Velocity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f64) -> Self::Output

Performs the - operation. Read more
Source§

impl VectorQuantity for Velocity

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V