Language Integrated Query in Rust (created by declarative macros).
This project is under development! API might be changed.
This is an example:
```rust use linq::linq; use linq::iter::Enumerable;
fn trylinqmethods() {
let x = 1..100;
let mut y: Vec
fn trylinqexpr() {
let x = 1..100;
let mut y: Vec
If you are familier with LINQ in C#, you will find this is easy to use.
The two imports is necessary:
rust
use linq::linq; // for `linq!` macro
use linq::iter::Enumerable; // for LINQ methods and `linq!` macro
The trait linq::iter::Enumerable
supports LINQ methods on Iterator
. You can find the correspondences below.
Iterator
in std.linq::iter
(but they are private so that you can't import them).The query expression begins with from
clause and ends with select
clause. Use ,
to seperate every clause.
rust
linq!(from x in coll, select x)
Now we supports these keywords:
select_many_single
)select_many
)rust
from <id> in <iter expr>,
Also you can enumerate elements of each set in the collection (Attention: for this type, you can't access the value that is in the first from
clause in select
clause):
```rust
let x = 1..5;
let y = vec![0, 0, 1, 0, 1, 2, 0, 1, 2, 3];
let e: Vec
assert_eq!(e, y); ```
If you want to zip or enumerate value-pairs of two sets, use zfrom
for the second from
:
```rust let x = 1..5; let y = vec![ (1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2), (4, 3), ]; let e: Vec<_> = linq!(from p in x.clone(), zfrom t in 0..p, select (p,t)).collect();
assert_eq!(e, y); ```
The expression in zfrom
recieve the cloned value in the first from
,
and the elements in two sets will be cloned for select
clause.
rust
where <expr>,
You can use where
clause in single-from query, and the expression will recieve a variable named the id
in from
clause. The expression need to return a boolean value.
rust
orderby <expr>,
orderby <expr>, descending,
You can use orderby
clause in single-from query. This query will collect the iterator, and sort them by the expression, then return the new iterator.
We need more unit-test samples. If you have any ideas, open issues to tell us.
Since the expression procedural macros is not stable, I only create macros by declarative macros.
sh
$ cargo test