Crates.io | kt-list-comprehensions |
lib.rs | kt-list-comprehensions |
version | |
source | src |
created_at | 2025-01-10 19:31:43.173929 |
updated_at | 2025-01-10 19:31:43.173929 |
description | Python-like list comprehensions for Rust |
homepage | |
repository | https://gitlab.com/ktanchev/kt-list-comprehensions |
max_upload_size | |
id | 1511639 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
After adding this crate to your dependencies, you can use
list comprehensions using the kt_list_comprehensions::list_comprehension![]
procedural macro.
for-in
clausesif
clausesuse kt_list_comprehensions::list_comprehension;
let vec = vec![-1, 2, 2, 3, 4];
let result: Vec<i32> = list_comprehension![x * 2 for x in vec if x > 0].collect();
assert_eq!(result, [4, 4, 6, 8]);
for-in
clausesuse kt_list_comprehensions::list_comprehension;
let vec_of_vectors = vec![vec![1, 2, 3], vec![4, 5, 6]];
let result: Vec<i32> = list_comprehension![
x
for vec in vec_of_vectors
for x in vec
].collect();
assert_eq!(result, [1, 2, 3, 4, 5, 6]);
if
clausesuse kt_list_comprehensions::list_comprehension;
let vec = vec!["1", "2", "not a number", "-3"];
let parse_i32 = |string: &str| string.parse::<i32>();
let result: Vec<i32> = list_comprehension![
parse_i32(number).unwrap()
for number in vec
if parse_i32(number).is_ok()
if parse_i32(number).unwrap() > 0
].collect();
assert_eq!(result, [1, 2]);
Dual-licensed under both the Apache License, Version 2.0 and the MIT License.
Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, will be dual-licensed as above, without any additional terms or conditions.