Struct with Fn element

I want to build a struct, in which an element is a function, and want to be able to display its contents as json, so I wrote the below:

#[macro_use] extern crate serde_derive;

#[derive(Serialize, Deserialize)]
struct Function<'a> {
    command: &'a str,
    f: fn(),
}

But got the below error:

error[E0277]: the trait bound `fn(): _IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` is not satisfied
  --> src\main.rs:61:5
   |
61 |     f: fn(),
   |     ^^^^^^^ the trait `_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` is not implemented for `fn()`
   |
   = note: required by `_IMPL_DESERIALIZE_FOR_Message::_serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `fn(): _IMPL_DESERIALIZE_FOR_Message::_serde::Deserialize<'_>` is not satisfied
  --> src\main.rs:61:5
   |
61 |     f: fn(),
   |     ^^^^^^^ the trait `_IMPL_DESERIALIZE_FOR_Message::_serde::Deserialize<'_>` is not implemented for `fn()`
   |
   = note: required by `_IMPL_DESERIALIZE_FOR_Message::_serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `fn(): _IMPL_DESERIALIZE_FOR_Message::_serde::Deserialize<'_>` is not satisfied
  --> src\main.rs:61:5
   |
61 |     f: fn(),
   |     ^^^^^^^ the trait `_IMPL_DESERIALIZE_FOR_Message::_serde::Deserialize<'_>` is not implemented for `fn()`
   |
   = note: required by `_IMPL_DESERIALIZE_FOR_Message::_serde::de::MapAccess::next_value`

You can't serialize or deserialize functions. What output would you expect from a serialized function?

I was trying to make "phrase / command" set, so that if the user picked a given phrase the matched command will be expected, then I thought it may be better to use HashMap and RegEx, so I came up with this issue

It ended that I used Vec<(Regex, fn)> instead of struct or HashMap

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.