# [−][src]Trait serde::de::Expected

    pub trait Expected {
        fn fmt(&self, formatter: &mut Formatter) -> Result;
    }

`Expected` represents an explanation of what data a `Visitor` was expecting to receive.

This is used as an argument to the `invalid_type`, `invalid_value`, and `invalid_length` methods of the `Error` trait to build error messages. The message should be a noun or noun phrase that completes the sentence "This Visitor expects to receive ...", for example the message could be "an integer between 0 and 64". The message should not be capitalized and should not end with a period.

Within the context of a `Visitor` implementation, the `Visitor` itself (`&self`) is an implementation of this trait.

    # use std::fmt;
    #
    # use serde::de::{self, Unexpected, Visitor};
    #
    # struct Example;
    #
    # impl<'de> Visitor<'de> for Example {
    #     type Value = ();
    #
    #     fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
    #         write!(formatter, "definitely not a boolean")
    #     }
    #
    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Err(de::Error::invalid_type(Unexpected::Bool(v), &self))
    }
    # }

Outside of a `Visitor`, `&"..."` can be used.

    # use serde::de::{self, Unexpected};
    #
    # fn example<E>() -> Result<(), E>
    # where
    #     E: de::Error,
    # {
    #     let v = true;
    return Err(de::Error::invalid_type(Unexpected::Bool(v), &"a negative integer"));
    # }

## Required methods

### `fn fmt(&self, formatter: &mut Formatter) -> Result`

Format an explanation of what data was being expected. Same signature as the `Display` and `Debug` traits.

Loading content...

## Trait Implementations

### `impl<'a> Display for dyn Expected + 'a`[src]

#### `fn fmt(&self, formatter: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

## Implementations on Foreign Types

### `impl<'a> Expected for &'a str`[src]

#### `fn fmt(&self, formatter: &mut Formatter) -> Result`[src]

Loading content...

## Implementors

### `impl<'de, T> Expected for T where
T: Visitor<'de>,` [src]

#### `fn fmt(&self, formatter: &mut Formatter) -> Result`[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`)