Crate word_filter[][src]

Expand description

A Word Filter for filtering text.

A Word Filter is a system for identifying and censoring specific words or phrases in strings. Common usage includes censoring vulgar or profane language and preventing spam or vandelism in user-provided content.

The Word Filter implementation provided here allows for advanced filtering functionality, including:

  • Finding and censoring filtered words.
  • Ignoring words that are considered “exceptions”.
  • Allowing specification of “aliases”, i.e. strings that can replace other strings (for example, an alias could be created to replace the letter “a” with the character “@”).
  • Ignoring specified separators (such as spaces or other characters) between letters of filtered words.


WordFilters must be created at compile-time using a build script using the tools provided in the codegen module. The generated code can then be include!ed and used.


For example, a simple WordFilter can be generated by the following.

First, add the word_filter crate to both the Cargo.toml [dependencies] and [build-dependencies] lists.

word_filter = "0.7.0"

word_filter = "0.7.0"

Next, generate the WordFilter in the file.

use std::{
    io::{BufWriter, Write},
use word_filter::codegen::{Visibility, WordFilterGenerator};

fn main() {
    let path = Path::new(&env::var("OUT_DIR").unwrap()).join("");
    let mut file = BufWriter::new(File::create(&path).unwrap());

        &mut file,

And finally, include the generated code in the file.

include!(concat!(env!("OUT_DIR"), "/"));

assert!(FILTER.censor("Should censor foo."), "Should censor ***.");


Macros for creating censors to be used in a WordFilter.

Tools for code generation.


Creates a censor replacing every grapheme with the given string.

Creates a sensor replacing the full matched words with the given string.


A word filter for identifying filtered words within strings.