Rust:
Deleting characters matching a pattern
How to:
In Rust, we can use the replace
method from the String
type or regex for more complex patterns. Here’s how you do it:
fn main() {
let phrase = "Hello, _world_! -- Programming in Rust --".to_string();
// Replace underscores with nothing
let cleaned = phrase.replace("_", "");
println!("{}", cleaned);
// Using regex for more complex patterns (remember to add regex crate to Cargo.toml)
let regex = regex::Regex::new(r"--.*?--").unwrap();
let s = regex.replace_all(&cleaned, "");
println!("{}", s);
}
// Output:
// Hello, world! -- Programming in Rust --
// Hello, world!
Deep Dive
Deleting characters matching a pattern isn’t unique to Rust; it’s a common operation in many programming languages. Historically, tools like sed
in Unix were used to transform text in powerful ways, and now languages provide built-in functions for string manipulation.
In Rust, the standard approach is using replace
for simple fixed patterns. For wildcards, repeats, or conditional removal, we turn to regex. The regex crate is the de facto tool for this, but remember, regex operations are more expensive in terms of performance, so use them judiciously.
Rust’s safety guarantees extend to text processing. While in some languages string manipulation can be a source of security vulnerabilities like buffer overflows, Rust’s design protects against such issues.
See Also
- The Rust
String
documentation: https://doc.rust-lang.org/std/string/struct.String.html regex
crate documentation: https://docs.rs/regex/- Rust Regex Book: https://rust-lang-nursery.github.io/regex/