Skip to content

The compiler doesn't suggest borrowing dbg!(x) #121795

Closed as not planned
Closed as not planned
@amab8901

Description

@amab8901
    let x = "Hello".to_string();
    dbg!(x);
    println!("{x}");

The above code returns this following error:

borrow of moved value: `x`
value borrowed here after move

To solve this, you'd normally have to add a reference character like this:

dbg!(&x);

But I think that Rust compiler should be able to understand what i'm trying to do without me having to manually add a reference on the variable x. I think this could be achieved by making an implementation that follows a similar logic as the following pseudocode:

fn dbg_reference_checker<T>(
    x: T
) -> Result<(), DbgError> {
    if !error_w_moved_value {
        use_moved_value(x)?;
    } else if !error_with_reference {
        use_reference(x)?;
    } else {
        return Err(DbgError::CannotDebugTheVariable)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions