PyInRs
A Rust type library that is as easy to use as Python built-in types.
1. Attribute
- Name: PyInRs.
- Language: Rust, requires version rustc >=
1.75.0. - Goal: Write a Rust type library that is as easy to use as Python built-in types.
- Module: List, Set, Dict, Int, Str, Deque, Fraction
- Style: Follow Rust's official recommended style.
- Test: Using rstest for unit testing and ensure that all tests passed.
- Security: There is no
unsafecode block. - Document: Using
cargo doc --opento open documents.
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 addition, deletion, modification, and inspection of containers.
- Efficiency: The performance of the parts with the same function as the standard library is almost the same.
3. Usage
To use it, add the following lines to your Cargo.toml file:
[]
= "1"
There are a total of 7 classes, refer to the 7 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 |
Deque<T> |
collections.deque |
Fraction |
fractions.Fraction |
Some simple examples:
use *;
// List support negative index
from; // 5
// List uniquify
from.uniquify; // [1, 2, 3]
// test whether a Set is proper subset of another Set
from < from; // true
// intersection of Sets, support intersection, union, difference, and symmetric difference
from & from; // {1, 3, 5}
// Dict access
from; // 1
// Dict get values as a Set
from.values.; // {1, 2, 3}
// Int modular power, very fast
from.pow; // 76
// Int factorial
from.factorial.factorial; // 66895029134491270575881180540903725867527463...
// Str split
from.split; // ["one", "two", "three"]
// Str join
from.join; // "192.168.0.1"
// Deque element reference
from.front; // 1
// Deque rotate to right (or left), very vivid!
from >> 1; // <5, 1, 2, 3, 4>
// Fraction addition
from + from; // 5/6
// Fraction modulo
from % from; // 1/6
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;
print!; // {"first": [123, 456], "second": [789], "third": [12345678987654321, 5]}
dict.keys.cloned.; // {"first", "second", "third"}
dict.factorial; // 120
// 2. All container types support iterators, such as:
for in from
// 3. Using pyinrs::Fraction in mymatrix to display accurate matrix.
use Matrix;
let a = from;
let b = create;
let c = create;
let d = eye;
print!;
/*
[[
-11/6 5/6;
5/3 -2/3;
]]
*/
If you want to use a similar library in C++, please see: PyInCpp.