PyInRs
A Rust type library that is as easy to use as Python built-in types.
1. Attribute
- Name: PyInRs (means Python in Rust)
- Goal: Provide a Rust type library that is as easy to use as Python built-in types
- Module: List, Set, Dict, Int, Str, Complex, Deque, Fraction, Decimal
2. Feature
- Simple: Stay simple, stay young. While ensuring friendly and robust, try to be concise and easy to maintain and read
- Friendly: With my careful design, it can be used as conveniently as Python's built-in types. Very Pythonic
- Robust: There are corresponding checks for the insert, remove, modify, and access of containers
- Efficient: The performance of the parts with the same function as the standard library is almost the same
- Secure: Tested using rstest and no
unsafecode block to ensure no security issues
3. Usage
To use it, add the following lines to your Cargo.toml file:
[]
= "1"
There are a total of 9 classes, refer to commonly used classes in Python:
| Type in PyInRs | Type in Python |
|---|---|
List<T> |
list |
Set<T> |
set |
Dict<K, V> |
dict |
Int |
int |
Str |
str |
Complex |
complex |
Deque<T> |
collections.deque |
Fraction |
fractions.Fraction |
Decimal |
decimal.Decimal |
Some simple examples:
use *;
// List support negative index
assert_eq!;
// List uniquify
assert_eq!;
// test whether a Set is proper subset of another Set
assert_eq!;
// intersection of Sets, support intersection, union, difference, and symmetric difference
assert_eq!;
// Dict access
assert_eq!;
// Dict get values as a Set
assert_eq!;
// Int basic operation, support +, -, *, /, % and compare
assert_eq!;
// Int increment, after my optimization, much faster than `+= 1`
assert_eq!;
// Int modular power, very fast
assert_eq!;
// Int factorial
assert_eq!;
// get random Int of specified number of digits
assert_eq!;
// calculate the next prime that greater than self
assert_eq!;
// calculate the tetration
assert_eq!;
// Str split
assert_eq!;
// Str join
assert_eq!;
// Complex addition
assert_eq!;
// Complex power
assert_eq!;
// Deque element reference
assert_eq!;
// Deque rotate to right (or left), very vivid!
assert_eq!;
// Fraction addition
assert_eq!;
// Fraction modulo
assert_eq!;
// Decimal calculate exact result
assert_eq!;
// Decimal keeps repeating parts exactly
assert_eq!;
4. Advantage
The advantage of PyInRs is that it combines the high performance of Rust with the ease of use of Python, and can also be easily combined with other libraries, for example:
use *;
// 1. All types can be printed and easily combined:
let dict: = .into;
assert_eq!;
assert_eq!;
assert_eq!;
// 2. All container types are iterable:
for in from
// 3. All immutable types are hashable:
use HashSet;
let _set1: = from;
let _set2: = from;
let _set3: = from;
let _set4: = from;
let _set5: = from;
// 4. Using pyinrs::Fraction in mymatrix to display accurate matrix.
use Matrix;
let a = from;
let b = zeros;
let c = ones;
let d = identity;
assert_eq!;
// 5. Using pyinrs::Decimal to calculate infinite cyclic decimals.
assert_eq!;
assert_eq!;
If you want to use a similar library in C++, please see: PyInCpp.