Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active March 26, 2023 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/d6e8f6ca69310b3f1916b6a92dbf7553 to your computer and use it in GitHub Desktop.
Save matthewjberger/d6e8f6ca69310b3f1916b6a92dbf7553 to your computer and use it in GitHub Desktop.
fn main() {
let s = "system/{id}/start/{mode}";
let substrings = extract_substrings(s);
assert_eq!(substrings, vec!["id", "mode"]);
let removed = remove_substrings(&s, &substrings);
assert_eq!(removed, "system/{}/start/{}");
}
fn extract_substrings(s: &str) -> Vec<&str> {
s.split('{')
.skip(1)
.filter_map(|substr| substr.split_once('}'))
.map(|(outer, _)| outer)
.collect()
}
fn remove_substrings(s: &str, substrings: &[&str]) -> String {
let mut result = String::from(s);
for substring in substrings {
result = result.replace(&format!("{{{}}}", substring), "{}");
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment