pub trait StringReplaceAll {
// Required method
fn replace_all<'a, P: Into<Pattern<'a>>>(
&self,
pattern: P,
replacement: &str,
) -> String;
}
Expand description
A trait that provides a replace_all
method for String
and str
types,
enabling both exact string and regex-based replacements.
Required Methods§
Sourcefn replace_all<'a, P: Into<Pattern<'a>>>(
&self,
pattern: P,
replacement: &str,
) -> String
fn replace_all<'a, P: Into<Pattern<'a>>>( &self, pattern: P, replacement: &str, ) -> String
Replaces all occurrences of a pattern with the given replacement.
This method supports:
- Exact string replacements (
&str
) - Regular expression-based replacements (
Regex
)
§Arguments
pattern
- The pattern to search for, which can be either:- A string slice (
&str
) for simple replacements. - A compiled regular expression (
Regex
) for pattern-based replacements.
- A string slice (
replacement
- The string that will replace occurrences of the pattern.
§Returns
A new String
with all occurrences replaced.
§Examples
§Using an exact string match
use string_replace_all::StringReplaceAll;
let text = "I think Ruth's dog is cuter than your dog!";
let result = text.replace_all("dog", "monkey");
assert_eq!(result, "I think Ruth's monkey is cuter than your monkey!");
§Using a regular expression match
use regex::Regex;
use string_replace_all::StringReplaceAll;
let text = "I think Ruth's dog is cuter than your dog!";
let regex = Regex::new("(?i)Dog").unwrap(); // Case-insensitive regex
let result = text.replace_all(®ex, "ferret");
assert_eq!(result, "I think Ruth's ferret is cuter than your ferret!");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl StringReplaceAll for str
Implementation of StringReplaceAll
for string slices (str
).
impl StringReplaceAll for str
Implementation of StringReplaceAll
for string slices (str
).
This allows direct use of .replace_all()
on &str
instances.
Source§fn replace_all<'a, P: Into<Pattern<'a>>>(
&self,
pattern: P,
replacement: &str,
) -> String
fn replace_all<'a, P: Into<Pattern<'a>>>( &self, pattern: P, replacement: &str, ) -> String
Replaces all occurrences of the given pattern in a &str
, returning a String
.
This implementation converts the string slice into a String
and calls
replace_all
on it.
§See also
replace_all
for details on arguments and behavior.
Source§impl StringReplaceAll for String
Implementation of StringReplaceAll
for String
.
impl StringReplaceAll for String
Implementation of StringReplaceAll
for String
.
This allows direct use of .replace_all()
on String
instances.
Source§fn replace_all<'a, P: Into<Pattern<'a>>>(
&self,
pattern: P,
replacement: &str,
) -> String
fn replace_all<'a, P: Into<Pattern<'a>>>( &self, pattern: P, replacement: &str, ) -> String
Replaces all occurrences of the given pattern in a String
.
§See also
replace_all
for details on arguments and behavior.