Skip to content

get_list

Returns key in dotenv content split on separator into a list, or default when it is absent.

Each item is trimmed of surrounding whitespace; empty items (a leading, trailing or doubled separator) are kept as empty strings rather than dropped, so the item count always matches the number of separators plus one.

use helpers4::env::get_list;

Cargo feature env (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features env

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.7", default-features = false, features = ["env"] }
pub fn get_list(content: &str, key: &str, separator: char, default: &[&str]) -> Vec<String>
ParameterTypeDescription
content&strThe dotenv text to read.
key&strThe variable name to look up.
separatorcharThe character that separates items, such as ,.
default&[&str]Returned when key is absent.

Vec<String> — The items, in order, or default.

use helpers4::env::get_list;

let content = "HOSTS=a.example.com, b.example.com,c.example.com\n";
assert_eq!(
    get_list(content, "HOSTS", ',', &["localhost"]),
    vec!["a.example.com", "b.example.com", "c.example.com"]
);
assert_eq!(get_list(content, "MISSING", ',', &["localhost"]), vec!["localhost"]);

src/env/get_list.rs