Rust:
Removing quotes from a string
How to:
fn remove_quotes(s: &str) -> String {
s.trim_matches(|c| c == '\"' || c == '\'').to_string()
}
fn main() {
let quoted_str = "\"Hello, Rustaceans!\"";
let cleaned_str = remove_quotes(quoted_str);
println!("{}", cleaned_str);
// Output: Hello, Rustaceans!
}
Sometimes you’ve got a string with mixed quotes, like this:
fn main() {
let mixed_quoted = "'Rust says: \"Hello, World!\"'";
let cleaned_str = remove_quotes(mixed_quoted);
println!("{}", cleaned_str);
// Output: Rust says: "Hello, World!"
}
Here, only the outermost single quotes are removed.
Deep Dive
When removing quotes from a string, you might wonder why it’s not just a simple .replace("\"", "")
. Early on, dealing with text was less standardized, and different systems had different ways of storing and transmitting text, often with some kind of ’escape sequence’ for special characters. Rust’s trim_matches
method is more versatile, allowing you to specify multiple characters to trim, and whether to trim from the start (prefix), the end (suffix), or both sides of the string.
There are alternatives, of course. Regex is the powerhouse for string manipulation, capable of matching complex patterns, and would be overkill for just removing quotes. Libraries like trim_in_place
could offer in-place trimming without the overhead of creating a new String
object, which could be desirable for performance-critical applications.
Under the hood, trim_matches
actually iterates through the characters of the string from both ends, checking against the provided pattern until a non-matching character is found. It’s efficient for what it does, but always be aware that it’s working with Unicode scalar values. If your string might contain multi-byte Unicode characters, you don’t have to worry about it breaking them up.
See Also
- Rust’s documentation on string manipulation: https://doc.rust-lang.org/book/ch08-02-strings.html
- The
regex
crate for complex patterns: https://crates.io/crates/regex - Rust by Example for practical coding scenarios: https://doc.rust-lang.org/stable/rust-by-example/std/str.html