Created
March 24, 2023 23:09
-
-
Save matthewjberger/743924b54ec1d1e7cc8e4c3d5aebf830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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