Computing the ASCII set at runtime seems unnecessary to me. I.e. what is the benefit using a for loop over creating your own constant by chaining .remove calls on NON_ALPHANUMERIC?
if want to .remove() 10 or 20 more chars NON_ALPHANUMERIC.remove(b' ').remove().remove()... 20 times long chain is hard to write and read. Is there a way to change it at runtime using for loop?
I disagree. But you can do something like this to compute your AsciiSet at both compile and run time using a while loop:
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC};
pub const fn my_ascii_set() -> AsciiSet {
const REM: &[u8] = b".:/-_?=%";
// We need to call `remove` here once to get an owned instance of `AsciiSet`
// from `NON_ALPHANUMERIC`, which is a `&AsciiSet`.
let mut set = NON_ALPHANUMERIC.remove(REM[0]);
let mut i = 1;
while i < REM.len() {
set = set.remove(REM[i]);
i += 1;
}
set
}
If you really don't want to do this at compile time, you can substitute the while loop with a for loop:
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC};
pub fn my_ascii_set() -> AsciiSet {
const REM: &[u8] = b".:/-_?=%";
// We need to call `remove` here once to get an owned instance of `AsciiSet`
// from `NON_ALPHANUMERIC`, which is a `&AsciiSet`.
let mut set = NON_ALPHANUMERIC.remove(REM[0]);
for c in &REM[1..] {
set = set.remove(*c);
}
set
}