Skip to content

Method as_ref not suggested when it should #55198

Closed
@KillTheMule

Description

@KillTheMule

As per https://www.reddit.com/r/rust/comments/9pb3ah/what_cool_stuff_have_you_learned_about_rust/e80zqpc/, I'm reporting a case where the method as_ref is not suggested by the compiler to use to convert an &Option<T> to an Option<&T>. Here's a playground link, the non-working code is

pub struct Keyword {}

pub struct ParsedLine<'a> {
  pub keyword: Option<&'a Keyword>,
}

impl<'a> From<&'a Option<Keyword>> for ParsedLine<'a>
{
  fn from(k: &'a Option<Keyword>) -> ParsedLine<'a>
  {
      ParsedLine{keyword: k}
  }
}
error[E0308]: mismatched types
  --> src/main.rs:13:27
   |
13 |       ParsedLine{keyword: k}
   |                           ^ expected enum `std::option::Option`, found reference
   |
   = note: expected type `std::option::Option<&Keyword>`
              found type `&'a std::option::Option<Keyword>`

The playground is using stable, I'm seeing this behavior on rustc 1.31.0-nightly (de3d640f5 2018-10-01) as well locally. If I should provide more information, let me know :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions