Skip to content

Diagnostics: compiler should hint about .as_ref() when trying to go from &Option<> to Option<&>. #53809

Closed
@vi

Description

@vi
fn main() {
    let a = Some(vec![1,2,3,4]);
    let b : &Option<Vec<u8>> = &a;
    let c : Option<&[u8]>;
    // c = b; // expected enum `std::option::Option`, found reference
    // c = *b; // expected &[u8], found struct `std::vec::Vec`
    // c = (*b).map(|x|x.as_slice()); // cannot move `b` out of borrowed content
    // c = b.map(|x|x.as_slice()); // cannot move `b` out of borrowed content and `x` does not live long enough
    
    // Ask IRC here
    
    c = b.as_ref().map(|x|x.as_slice());

    let _ = c;
}

Compiler message should hint Option::as_ref, to shift user attention from object inside Option to the Option itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-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