Anstyle in rustc_errors - Rust
Description: ANSI Text styling

Skip to main content

## Anstyle

# Struct Anstyle

Source

    pub struct Anstyle {
        fg: Option<Color>,
        bg: Option<Color>,
        underline: Option<Color>,
        effects: Effects,
    }

Expand description

ANSI Text styling

You can print a `Style` to render the corresponding ANSI code. Using the alternate flag `#` will render the ANSI reset code, if needed. Together, this makes it convenient to render styles using inline format arguments.

## §Examples

    let style = anstyle::Style::new().bold();

    let value = 42;
    println!("{style}{value}{style:#}");

## Fields§

§`fg: Option<Color>`§`bg: Option<Color>`§`underline: Option<Color>`§`effects: Effects`

## Implementations§

Source§

### impl Style

#### §Core

Source

#### pub const fn new() -> Style

No effects enabled

##### §Examples

    let style = anstyle::Style::new();

Source

#### pub const fn fg_color(self, fg: Option<Color>) -> Style

Set foreground color

##### §Examples

    let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn bg_color(self, bg: Option<Color>) -> Style

Set background color

##### §Examples

    let style = anstyle::Style::new().bg_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn underline_color(self, underline: Option<Color>) -> Style

Set underline color

##### §Examples

    let style = anstyle::Style::new().underline_color(Some(anstyle::AnsiColor::Red.into()));

Source

#### pub const fn effects(self, effects: Effects) -> Style

Set text effects

##### §Examples

    let style = anstyle::Style::new().effects(anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE);

Source

#### pub fn render(self) -> impl Display + Copy

Render the ANSI code

`Style` also implements `Display` directly, so calling this method is optional.

Source

#### pub fn write_to(self, write: &mut dyn Write) -> Result<(), Error>

Write the ANSI code

Source

#### pub fn render_reset(self) -> impl Display + Copy

Renders the relevant `Reset` code

Unlike `Reset::render`, this will elide the code if there is nothing to reset.

Source

#### pub fn write_reset_to(self, write: &mut dyn Write) -> Result<(), Error>

Write the relevant `Reset` code

Unlike `Reset::render`, this will elide the code if there is nothing to reset.

Source§

### impl Style

#### §Convenience

Source

#### pub const fn bold(self) -> Style

Apply `bold` effect

##### §Examples

    let style = anstyle::Style::new().bold();

Source

#### pub const fn dimmed(self) -> Style

Apply `dimmed` effect

##### §Examples

    let style = anstyle::Style::new().dimmed();

Source

#### pub const fn italic(self) -> Style

Apply `italic` effect

##### §Examples

    let style = anstyle::Style::new().italic();

Source

#### pub const fn underline(self) -> Style

Apply `underline` effect

##### §Examples

    let style = anstyle::Style::new().underline();

Source

#### pub const fn blink(self) -> Style

Apply `blink` effect

##### §Examples

    let style = anstyle::Style::new().blink();

Source

#### pub const fn invert(self) -> Style

Apply `invert` effect

##### §Examples

    let style = anstyle::Style::new().invert();

Source

#### pub const fn hidden(self) -> Style

Apply `hidden` effect

##### §Examples

    let style = anstyle::Style::new().hidden();

Source

#### pub const fn strikethrough(self) -> Style

Apply `strikethrough` effect

##### §Examples

    let style = anstyle::Style::new().strikethrough();

Source§

### impl Style

#### §Reflection

Source

#### pub const fn get_fg_color(self) -> Option<Color>

Get the foreground color

Source

#### pub const fn get_bg_color(self) -> Option<Color>

Get the background color

Source

#### pub const fn get_underline_color(self) -> Option<Color>

Source

#### pub const fn get_effects(self) -> Effects

Source

#### pub const fn is_plain(self) -> bool

Check if no styling is enabled

## Trait Implementations§

Source§

### impl BitOr<Effects> for Style

#### §Examples

    let style = anstyle::Style::new() | anstyle::Effects::BOLD.into();

Source§

#### type Output = Style

The resulting type after applying the `|` operator.
Source§

#### fn bitor(self, rhs: Effects) -> Style

Performs the `|` operation. Read more
Source§

### impl BitOrAssign<Effects> for Style

#### §Examples

    let mut style = anstyle::Style::new();
    style |= anstyle::Effects::BOLD.into();

Source§

#### fn bitor_assign(&mut self, other: Effects)

Performs the `|=` operation. Read more
Source§

### impl Clone for Style

Source§

#### fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

#### fn clone_from(&mut self, source: &Self)

Performs copy-assignment from `source`. Read more
Source§

### impl Copy for Style

Source§

### impl Debug for Style

Source§

#### fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

### impl Default for Style

Source§

#### fn default() -> Style

Returns the “default value” for a type. Read more
Source§

### impl Display for Style

Source§

#### fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

### impl Eq for Style

Source§

### impl From<Effects> for Style

#### §Examples

    let style: anstyle::Style = anstyle::Effects::BOLD.into();

Source§

#### fn from(effects: Effects) -> Style

Converts to this type from the input type.
Source§

### impl Hash for Style

Source§

#### fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given `Hasher`. Read more
1.3.0 · Source§

#### fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given `Hasher`. Read more
Source§

### impl Ord for Style

Source§

#### fn cmp(&self, other: &Style) -> Ordering

This method returns an `Ordering` between `self` and `other`. Read more
1.21.0 (const: unstable) · Source§

#### fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

#### fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

#### fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

### impl PartialEq for Style

Source§

#### fn eq(&self, other: &Style) -> bool

Tests for `self` and `other` values to be equal, and is used by `==`.
1.0.0 (const: unstable) · Source§

#### fn ne(&self, other: &Rhs) -> bool

Tests for `!=`. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

### impl PartialEq<Effects> for Style

#### §Examples

    let effects = anstyle::Effects::BOLD;
    assert_eq!(anstyle::Style::new().effects(effects), effects);
    assert_ne!(anstyle::Effects::UNDERLINE | effects, effects);
    assert_ne!(anstyle::RgbColor(0, 0, 0).on_default() | effects, effects);

Source§

#### fn eq(&self, other: &Effects) -> bool

Tests for `self` and `other` values to be equal, and is used by `==`.
1.0.0 (const: unstable) · Source§

#### fn ne(&self, other: &Rhs) -> bool

Tests for `!=`. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

### impl PartialOrd for Style

Source§

#### fn partial_cmp(&self, other: &Style) -> Option<Ordering>

This method returns an ordering between `self` and `other` values if one exists. Read more
1.0.0 (const: unstable) · Source§

#### fn lt(&self, other: &Rhs) -> bool

Tests less than (for `self` and `other`) and is used by the `<` operator. Read more
1.0.0 (const: unstable) · Source§

#### fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more
1.0.0 (const: unstable) · Source§

#### fn gt(&self, other: &Rhs) -> bool

Tests greater than (for `self` and `other`) and is used by the `>` operator. Read more
1.0.0 (const: unstable) · Source§

#### fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more
Source§

### impl StructuralPartialEq for Style

Source§

### impl Sub<Effects> for Style

#### §Examples

    let style = anstyle::Style::new().bold().underline() - anstyle::Effects::BOLD.into();

Source§

#### type Output = Style

The resulting type after applying the `-` operator.
Source§

#### fn sub(self, other: Effects) -> Style

Performs the `-` operation. Read more
Source§

### impl SubAssign<Effects> for Style

#### §Examples

    let mut style = anstyle::Style::new().bold().underline();
    style -= anstyle::Effects::BOLD.into();

Source§

#### fn sub_assign(&mut self, other: Effects)

Performs the `-=` operation. Read more

## Auto Trait Implementations§

§

### impl DynSend for Style

§

### impl DynSync for Style

§

### impl Freeze for Style

§

### impl RefUnwindSafe for Style

§

### impl Send for Style

§

### impl Sync for Style

§

### impl Unpin for Style

§

### impl UnsafeUnpin for Style

§

### impl UnwindSafe for Style

## Blanket Implementations§

§

### impl<T> Aligned for T

§

#### const ALIGN: Alignment

Alignment of `Self`.
Source§

### impl<T> Any for T
where T: 'static + ?Sized,

Source§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
Source§

### impl<T> AnyEq for T
where T: Any + PartialEq,

Source§

#### fn equals(&self, other: &(dyn Any + 'static)) -> bool

Source§

#### fn as_any(&self) -> &(dyn Any + 'static)

Source§

### impl<T> Borrow<T> for T
where T: ?Sized,

Source§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

### impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

### impl<T> CloneToUninit for T
where T: Clone,

Source§

#### unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (`clone_to_uninit`)
Performs copy-assignment from `self` to `dest`. Read more
Source§

### impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn compare(&self, key: &K) -> Ordering

Compare self to `key` and return their ordering.
Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Compare self to `key` and return `true` if they are equal.
Source§

### impl<T> ErasedDestructor for T
where T: 'static,

Source§

### impl<T> From<T> for T

Source§

#### fn from(t: T) -> T

Returns the argument unchanged.

Source§

### impl<T> Instrument for T

Source§

#### fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided `Span`, returning an `Instrumented` wrapper. Read more
Source§

#### fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current `Span`, returning an `Instrumented` wrapper. Read more
Source§

### impl<T, U> Into<U> for T
where U: From<T>,

Source§

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

Source§

### impl<T> IntoEither for T

Source§

#### fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left` is `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
Source§

#### fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left(&self)` returns `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
Source§

### impl<T> Pointable for T

Source§

#### const ALIGN: usize

The alignment of pointer.
Source§

#### type Init = T

The type for initializers.
Source§

#### unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

#### unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

#### unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

#### unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

### impl<T> Same for T

Source§

#### type Output = T

Should always be `Self`
Source§

### impl<T> ToOwned for T
where T: Clone,

Source§

#### type Owned = T

The resulting type after obtaining ownership.
Source§

#### fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

#### fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

### impl<T> ToString for T
where T: Display + ?Sized,

Source§

#### fn to_string(&self) -> String

Converts the given value to a `String`. Read more
Source§

### impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

#### type Error = Infallible

The type returned in the event of a conversion error.
Source§

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

### impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

### impl<T> WithSubscriber for T

Source§

#### fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided `Subscriber` to this type, returning a `WithDispatch` wrapper. Read more
Source§

#### fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default `Subscriber` to this type, returning a `WithDispatch` wrapper. Read more

## Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain `repr(...)` attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 14 bytes