Skip to main content

DisplayWidth

Trait DisplayWidth 

Source
pub trait DisplayWidth {
    // Required methods
    fn display_width(&self) -> usize;
    fn display_width_with_options(&self, options: StringWidthOptions) -> usize;
}
Expand description

Trait for types that can have their display width calculated

This trait provides a clean API for calculating string width with better naming than the original StringWidthInput.

Required Methods§

Source

fn display_width(&self) -> usize

Calculate the display width using default options

Source

fn display_width_with_options(&self, options: StringWidthOptions) -> usize

Calculate the display width with custom options

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl DisplayWidth for String

Source§

fn display_width(&self) -> usize

Calculate the display width using default options

§Examples
use string_width::DisplayWidth;

let text = String::from("Hello 🌍");
assert_eq!(text.display_width(), 8);
Source§

fn display_width_with_options(&self, options: StringWidthOptions) -> usize

Calculate the display width with custom options

§Arguments
  • options - Configuration options for the calculation
§Examples
use string_width::{DisplayWidth, StringWidthOptions};

let text = String::from("±×÷");
let options = StringWidthOptions::builder()
    .ambiguous_as_wide()
    .build();
assert_eq!(text.display_width_with_options(options), 6);
Source§

impl DisplayWidth for str

Source§

fn display_width(&self) -> usize

Calculate the display width using default options

§Examples
use string_width::DisplayWidth;

assert_eq!("Hello".display_width(), 5);
assert_eq!("😀".display_width(), 2);
Source§

fn display_width_with_options(&self, options: StringWidthOptions) -> usize

Calculate the display width with custom options

§Arguments
  • options - Configuration options for the calculation
§Examples
use string_width::{DisplayWidth, StringWidthOptions};

let options = StringWidthOptions::builder()
    .count_ansi(true)
    .build();
assert_eq!("\x1b[31mRed\x1b[0m".display_width_with_options(options), 12);

Implementors§