Skip to content

Closures cause unnecessary borrows #23591

Closed
@mpdn

Description

@mpdn

Simple example:

use std::cmp::Ordering;

fn something<T,F>(slice: &mut [T], mut compare: F) where
    F: FnMut(&T,&T) -> Ordering
{
    // This compiles fine:
    if compare(&slice[0], &slice[1]) == Ordering::Less {
        slice.swap(0, 1);
    }

    // But this does not:
    let mut cmp = |a,b| compare(a,b) == Ordering::Less;

    if cmp(&slice[0], &slice[1]) {
        slice.swap(0, 1);
    }
}

Playpen: http://is.gd/niaLAa

It seems like it marks the closure return value as a borrowed value from slice, even though it is just a bool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.fixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions