iter-python
Python generator expressions (i!
) and "list" comprehensions (v!
)

Usage
-
Add the following line to your Cargo.toml
, under [dependencies]
:
iter-python = "0.10.0"
-
Bring i!
and (enhanced) v!
ec into scope in you Rust code with:
use ::iter_python::prelude::*;
Example
use ::iter_python::prelude::{*,
v as vec,
};
fn main ()
{
let infinite_odds = || i!(2 * n + 1 for n in 0..);
let sums_of_odds = i!(infinite_odds().take(n).sum() for n in 1..);
assert!(sums_of_odds.take(100).all(is_square));
let v = v![
2 * x
for &x_opt in &[None, Some(21), None]
if let Some(x) = x_opt
];
assert_eq!(
dbg!(v),
vec![42], );
const MATRIX: &str = "\
+-----+-----+-----+-----+-----+
| a11 | a12 | a13 | a14 | a15 |
| a21 | a22 | a23 | a24 | a25 |
| a31 | a32 | a33 | a34 | a35 |
| a41 | a42 | a43 | a44 | a45 |
| a51 | a52 | a53 | a54 | a55 |
+-----+-----+-----+-----+-----+";
const N: usize = 6;
use ::iter_python::macros::lazy_format; let line = || lazy_format!(
"+-{}-+",
"-+-".join(i!("---" for _ in 1..N))
);
let top_line = line();
let body = "\n".join(i!(
lazy_format!(
"| {} |",
" | ".join(i!(f!("a{i}{j}") for j in 1..N)),
)
for i in 1..N
));
let bottom_line = line();
let matrix = format!("{top_line}\n{body}\n{bottom_line}");
assert_eq!(matrix, MATRIX);
}
fn is_square (n: u32)
-> bool
{
n == ((n as f64).sqrt().trunc() as u32).pow(2)
}
See iter!
and vec!
for more examples.
no_std
support
This crates supports #![no_std]
, by disabling the default "std"
feature.