muppi090909
muppi090909

Reputation: 121

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. 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

Upvotes: 1

Views: 331

Answers (1)

Rob Napier
Rob Napier

Reputation: 299285

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.

Upvotes: 6

Related Questions