#closures #check #literals #string #character #proc-macro #generate

macro in_str

A procedural macro to generate a closure that checks if a character is in the provided literal string

2 releases

new 0.1.1 Jan 28, 2025
0.1.0 Nov 20, 2024

#138 in #literals

Download history 141/week @ 2024-11-20 5/week @ 2024-11-27 8/week @ 2024-12-04 7/week @ 2024-12-11 54/week @ 2025-01-22

54 downloads per month
Used in whitehole

MIT license

5KB

in_str!

license Crates.io Version docs.rs

A procedural macro to generate a closure that checks if a character is in the provided literal string.

use in_str::in_str;

let _ = in_str!("abc");
// equals to
let _ = |c: char| matches!(c, 'a' | 'b' | 'c');
// usually faster than
let _ = |c: char| "abc".contains(c);

// escape will be handled automatically
let _ = in_str!("\n\u{10ffff}");
// equals to
let _ = |c: char| matches!(c, '\n' | '\u{10ffff}');

// also works with byte strings
let _ = in_str!(b"abc");
// equals to
let _ = |c: u8| matches!(c, b'a' | b'b' | b'c');
// escape will be handled automatically
let _ = in_str!(b"\n\xff");
// equals to
let _ = |c: u8| matches!(c, b'\n' | 0xff);

Dependencies

~1.5MB
~38K SLoC