Skip to content

help, pass closure variable to Ref::map function failed  #56284

@code-brewer

Description

@code-brewer

The demo code is:

#![feature(refcell_map_split)]
use std::cell::{Ref, RefCell};    

fn main() { 
    let cell = RefCell::new([1, 2, 3, 4,5]);
    let borrow = cell.borrow(); 
     //    let (begin, end) = Ref::map_split(borrow, |s| s.split_at(3));   // OK

    // extract closure as a variable then pass as argument, raise error.
    let lambda = |s: &[i32;5]| s.split_at(3);
    let (begin, end) = Ref::map_split(borrow, lambda);

    assert_eq!(*begin, [1, 2, 3]);
    assert_eq!(*end, [4, 5]);
}

It complains error when run:

error[E0271]: type mismatch resolving `for<'r> <[closure@examples/dddd.rs:10:18: 10:45] as std::ops::FnOnce<(&'r [i32; 5],)>>::Output == (&'r _, &'r _)`
  --> examples/dddd.rs:11:24
   |
11 |     let (begin, end) = Ref::map_split(borrow, lambda);
   |                        ^^^^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
   |
   = note: required by `<std::cell::Ref<'b, T>>::map_split`

How to fix this while keep passing closure as an argument to Ref::map_split ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions