Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Created March 24, 2023 23:09
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/743924b54ec1d1e7cc8e4c3d5aebf830 to your computer and use it in GitHub Desktop.
Save matthewjberger/743924b54ec1d1e7cc8e4c3d5aebf830 to your computer and use it in GitHub Desktop.
fn main() {
let input = "Some string {} another value {} third value {}";
let output = numbered_placeholders(input);
let expected_output = "Some string {0} another value {1} third value {2}";
assert_eq!(output, expected_output);
}
fn numbered_placeholders(s: &str) -> String {
let mut counter = 0;
let mut result = String::new();
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '{' && chars.peek() == Some(&'}') {
result.push_str(&format!("{{{}}}", counter));
counter += 1;
chars.next(); // consume the '}' character
} else {
result.push(c);
}
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment