Crates.io | set_derive |
lib.rs | set_derive |
version | 1.1.0 |
source | src |
created_at | 2023-03-16 16:21:32.542058 |
updated_at | 2024-01-09 08:23:12.944412 |
description | Using Macros to Implement List comprehension Similar to Python Language |
homepage | https://github.com/A4-Tacks/rust-set-derive |
repository | https://github.com/A4-Tacks/rust-set-derive |
max_upload_size | |
id | 811631 |
size | 20,647 |
Vec [v]
, HashMap {k=>v}
and HashSet !{k}
have been implementedOrdinary element: [foo, 1, 2i32]
for each: [*(i; for i in 0..10)]
for each and filter: [*(i; for i in 0..10; if i != 5)]
for each and filter and sub for each:
[*(i + j; for i in 0..10; if i != 5; for j in 1..i)]
Ordinary element: {k => v, [k1]: v1}
String ordinary key: {%[key]: "jack"}
(key: String
)
Ident key: {name: "jack"}
(name: &'static str
)
String ident key: {%name: "jack"}
(name: String
)
Variable key: {*key: "jack"}
(let key = "name";
)
Literal key: {"name": "jack"}
Literal key tostring: {%"name": "jack"}
(name: String
)
for each: Just like in Vec, but slightly different.
e.g {*(i => i+1; for i in 0..5)}
for each reserve: [*[10](i; for i in 0..10)]
generic params: <i64>[1, 2, 3]
init with capacity: (20)[1, 2, 3]
| <i64>(20)[1, 2, 3]
inspect block: [*(i; for i in 0..10; {dbg!(i);} if i & 1 == 0)]
crate: https://crates.io/crates/set_derive
# use set_derive::set_derive;
# use std::collections::HashMap;
let arr1 = vec![
0, 1, 8, 9, 10, 15, 16, 17, 18, 19,
];
let arr2 = set_derive!([
0, 1,
*(i; for i in 8..=10),
*(i; for i in 15..20),
]);
// python: [0, 1, *range(8, 10+1), *range(15, 20)]
assert_eq!(arr1, arr2);
let arr = set_derive!([*(
i+j;
for i in 0..6;
if i%2!=0;
for j in 0..i;
)]);
// python: [i+j for i in range(6) if i%2 for j in range(0, i)]
assert_eq!(arr, vec![1, 3, 4, 5, 5, 6, 7, 8, 9]);
let age_key = "age";
let jack = set_derive!({
name: "jack",
*age_key: "20",
});
assert_eq!(jack, HashMap::from([("name", "jack"), ("age", "20")]));