#numbers #absolute #values #drop #avoiding #immutability #abs-n

signed

一个用于处理有符号数字的绝对值,避免混淆的库

2 个版本

0.1.1 2023 年 2 月 16 日
0.1.0 2023 年 2 月 16 日

数学 中排名 #810

MIT 许可证

11KB
183

方便和人性化的交互方式,用于处理有符号数字及其部分

除了 std (即将推出 no_std) 以外,该 Crates 没有其他依赖项。

该 Crates 还包含一个小型 doctest 和所有函数的 criterion 基准测试。

use signed::Signed;
                                                                                                            
let mut n: i32 = 42; // A number to test operations with it
                                                                                                            
let mut abs_n = n.get_absolute(); // Gets a "pointer" to an absolute value of a number
                                                                                                            
abs_n -= &2;
drop(abs_n);
                                                                                                            
assert_eq!(n, 40);
                                                                                                            
let mut abs_n = n.get_absolute(); // Gets a "pointer" to an absolute value of a number
                                                                                                            
abs_n += &6;
drop(abs_n);
                                                                                                            
assert_eq!(n, 46);
                                                                                                            
n = -42; // Let's try with negative number
                                                                                                            
// Unfortunately, due to assertions, abs_n has to be dropped before immutable use, but when n
// is changed, abs_n is changed accordingly
                                                                                                            
let mut abs_n = n.get_absolute(); // Gets a "pointer" to an absolute value of a number
                                                                                                            
abs_n -= &4;
drop(abs_n);
                                                                                                            
assert_eq!(n, -38); // Now that's different, we subtracted, but got a number *bigger* than the initial one.
                                                                                                            
let mut abs_n = n.get_absolute(); // Gets a "pointer" to an absolute value of a number
                                                                                                            
abs_n += &6;
drop(abs_n);
                                                                                                            
assert_eq!(n, -44); // And by adding to the absolute value, we get a number *smaller*.
                                                                                                            
// All of this works for i8, i16, i32, i64 and i128!

无运行时依赖项