Home > Back-end >  Is there any way to get a function pointer from a &str type in rust which is provided by the use
Is there any way to get a function pointer from a &str type in rust which is provided by the use

Time:12-20

Is there any way to get a function pointer from a &str type in rust which is provided by the use. Example: use provides a name of a function provided by the user i need some way to call the function preferably a closure or a function pointer

CodePudding user response:

Rust is a static, compiled language. There is no access to functions by name at runtime. The functions may not even exist (they may be inlined, for example, or optimized away). Instead, what you need to do is create a HashMap of Strings to functions you would like to call at runtime, or use a match to dispatch by string.

  • Related