Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A helpful resource for better understanding the Cow enum is The Secret Life of Cows. Be sure to click on the “playground” links in the discussion there on the serde crate.

...

Step-by-step, how to understand the describe() function example

  1. Consider two traits

    1. From<String>

    2. From<&'a str>

  2. Cow<'a, str> implements both traits

    1. Cow has the associated function
      fn from(s: String) → Cow<'a str>

    2. Cow has another associated function
      fn from(s: &'a str) → Cow<'a str>

  3. Consider the additional trait

    1. Into<'a, Cow<'a str>>

  4. String automatically implements Into, as does str, given that the From traits are implemented

  5. Both String and str implement Into with a method of the following form

    1. fn into(self) → Cow<a' str>

  6. In the first four arms of the match expression, there are string literals upon which the method into() is called. We know the methods must have a return type of Cow<'static str>.

  7. A string literal is of type &str, a shared reference to a string slice.

  8. When applying the method into(), the string literal is dereferenced into a string slice str

  9. Then the method into() can be applied on the str. We have already seen that this method exists.