A fn() doesn't implement fmt::Pointer?

The docs about fn says "Function pointers implement the following traits: [...] Pointer". But when I try to use this, I get an error. This is my code:

use std::fmt::Pointer;

fn f() {}

fn main() {
    let _: &dyn Pointer = &f as &dyn Pointer;
}

It produces an error when compiling:

error[E0277]: the trait bound `fn() {f}: Pointer` is not satisfied
 --> src/main.rs:6:31
  |
6 |     let _: Box<dyn Pointer> = Box::new(f);
  |                               ^^^^^^^^^^^ the trait `Pointer` is not implemented for `fn() {f}`
  |
  = note: required for the cast to the object type `dyn Pointer`

Does fn() implement Pointer or not?

Function pointers implement it, but function items do not. This works:

use std::fmt::Pointer;

fn f() {}

fn main() {
    let func: fn() = f;
    let _: &dyn Pointer = &func as &dyn Pointer;
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.