Skip to content

&fn() should be copied, not moved #8331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stepancheg opened this issue Aug 6, 2013 · 2 comments
Closed

&fn() should be copied, not moved #8331

stepancheg opened this issue Aug 6, 2013 · 2 comments

Comments

@stepancheg
Copy link
Contributor

Code:

fn baz(f: &fn()) {
    let g = f;
    let h = f;
}

fails with:

/.../tmp.rs:3:12: 3:13 error: use of moved value: `f`
/.../tmp.rs:3     let h = f;
                  ^
/.../tmp.rs:2:12: 2:13 note: `f` moved here because it has type `&fn<no-bounds>()`, which is moved by default (use `copy` to override)
/.../tmp.rs:2     let g = f;

Similar problem:

fn foo(fs: &[&fn()]) {
    let f = fs[0];
}

Fails with:

/.../tmp.rs:2:12: 2:16 error: cannot move out of dereference of & pointer
/.../tmp.rs:2     let f = fs[0];

Because of this it seems to be impossible to iterate over &[&fn()].

@stepancheg
Copy link
Contributor Author

Sorry for inconvenience, I've found a solution (for master):

fn bar(f: &fn()) {}

fn foo(f: &fn()) {
    bar(||f());
    bar(||f());
}

fn baz(fs: &[&fn()]) {
    for f in fs.iter() {
        bar(||(*f)());
    }
}

@huonw
Copy link
Member

huonw commented Aug 6, 2013

There's an explanation of why this is so here.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 27, 2022
fix bad suggestion on `numeric_literal`

closes rust-lang#8331

changelog: [`numeric_literal`]  fix suggestion not showing sign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants