# [−] [src] Trait regex::bytes::Replacer

    pub trait Replacer {
        fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>);

        fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> { ... }
        fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self> { ... }
    }

Replacer describes types that can be used to replace matches in a byte string.

In general, users of this crate shouldn't need to implement this trait, since implementations are already provided for `&[u8]` and `FnMut(&Captures) -> Vec<u8>` (or any `FnMut(&Captures) -> T` where `T: AsRef<[u8]>`), which covers most use cases.

## Required methods

### `fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>)`

Appends text to `dst` to replace the current match.

The current match is represented by `caps`, which is guaranteed to have a match at capture group `0`.

For example, a no-op replacement would be `dst.extend(&caps[0])`.

Loading content...

## Provided methods

### `fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>>`

Return a fixed unchanging replacement byte string.

When doing replacements, if access to `Captures` is not needed (e.g., the replacement byte string does not need `$` expansion), then it can be beneficial to avoid finding sub-captures.

In general, this is called once for every call to `replacen`.

### `fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>`

Return a `Replacer` that borrows and wraps this `Replacer`.

This is useful when you want to take a generic `Replacer` (which might not be cloneable) and use it without consuming it, so it can be used more than once.

# Example

    use regex::bytes::{Regex, Replacer};

    fn replace_all_twice<R: Replacer>(
        re: Regex,
        src: &[u8],
        mut rep: R,
    ) -> Vec<u8> {
        let dst = re.replace_all(src, rep.by_ref());
        let dst = re.replace_all(&dst, rep.by_ref());
        dst.into_owned()
    }

Loading content...

## Implementations on Foreign Types

### `impl<'a> Replacer for &'a [u8]` [src]

#### `fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>)` [src]

#### `fn no_expansion(&mut self) -> Option<Cow<[u8]>>` [src]

#### `fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>` [src]

Loading content...

## Implementors

### `impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R>` [src]

#### `fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>)` [src]

#### `fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>>` [src]

#### `fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>` [src]

### `impl<'t> Replacer for NoExpand<'t>` [src]

#### `fn replace_append(&mut self, _: &Captures, dst: &mut Vec<u8>)` [src]

#### `fn no_expansion(&mut self) -> Option<Cow<[u8]>>` [src]

#### `fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>` [src]

### `impl<F, T> Replacer for F where
F: FnMut(&Captures) -> T,
T: AsRef<[u8]>,` [src]

#### `fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>)` [src]

#### `fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>>` [src]

#### `fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>` [src]

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)