#env-var #string-formatting #string #environment #formatting #batch #windows

expand_str

ExpandEnvironmentStrings-like function and string formatting utility

2 releases

0.1.1 Jun 18, 2020
0.1.0 Oct 15, 2019

#236 in Value formatting

Download history 114/week @ 2024-04-08 296/week @ 2024-04-15 209/week @ 2024-04-22 36/week @ 2024-04-29 239/week @ 2024-05-06 168/week @ 2024-05-13 168/week @ 2024-05-20 100/week @ 2024-05-27 105/week @ 2024-06-03 138/week @ 2024-06-10 139/week @ 2024-06-17 117/week @ 2024-06-24 152/week @ 2024-07-01 105/week @ 2024-07-08 316/week @ 2024-07-15 124/week @ 2024-07-22

701 downloads per month
Used in path-cleaner

WTFPL license

11KB
206 lines

expand_str

ExpandEnvironmentStrings emulation with more strict input checking and a way to substitute your own variable values.

Example

Add expand_str to your dependencies of your Cargo.toml:

[dependencies]
expand_str = "0.1"

And then, in your rust file:


fn main() {
    println!("{}", expand_str::expand_string_with_env("This is what's in your PATH: %PATH%").unwrap());
}

If you only want string formatting, environment variable access code can be dropped, e.g.:

[dependencies.expand_str]
version = "0.1"
default-features = false

(Exact feature name is env, but there are no other features at the moment).

Formatting-only example:

fn main() {
    let values = {
        let mut values = HashMap::new();
        values.insert("DRINK", "cup of tea");
        values.insert("FOOD", "cookies");
        values
    };

    let src = "Here is a %DRINK% and some %FOOD%.";
    let x = expand_string_with_values(src, |id| values.get(id)).unwrap();
    println!("{}", x);
}

No runtime deps

Features