Versions Compared

Key

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

...

(assuming I understand this, which is an if, a small if, but an if nonetheless)

Part A

...

We can implement methods directly on shared reference types themselves. There is no deref coercion then when called on a shared reference that is of that shared reference type (playground)

...

Note how the implementation is for the ref string slice type, not the primitive string slice type.

Part B

...

  1. Consider two traits

    1. From<String>

    2. From<&'a static str>

  2. The enum Cow<'astatic, str> implements both traits

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

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

  3. Consider another trait

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

  4. A From implementation implies that there is a

    , str>
  5. Therefore String implements<

  6. Consider the additional trait

    Into<Cow<'a, str>>

    corresponding Into implementation. There is a blanket implementation of Into in the standard library. My guess is that the following code is the blanket implementation

    Code Block
    languagerust
    impl<T,U> Into<U> for T
       where U: From<T> {
       
      fn into(self) -> U {
        U::from(self)
      }  
    }
  7. Therefore String implements the Into<Cow<'static,str>> trait

    Code Block
    languagerust
    impl Into<Cow<'static, str>> for String {
      fn into(self) -> Cow<'static, str> {
        ...
      }
    }

  1. Consider the additional trait

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

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

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

  4. 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>.

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

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

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