Function call by String name


How can I call function by String name??


Can you say a little bit more about what you want to do? Maybe something like this:


Most likely by figuring out a different way to call your functions without involving string names. But how you do this depends a lot on the underlying problem you want to solve. Can you tell us more about that? Why do you want to call functions by their name?


if i have json function name and i want to call function by string name


The suggestion by @gregwtmtno should get the job done quickly.

Personally, I would introduce an intermediate enum type where each possible function is listed. This is especially nice if parsing/validation and execution take place at different times. But it does create some boilerplate.


maybe hash map . just thought ?


How many different functions do you have? If it is only a dozen, then I wouldn’t bother with a hashmap. If you really want to use one, you should be able to store boxed closures. (Maybe even unboxed ones, I don’t know about that)


the code build map every time we call it.
if function have variables.


Your link doesn’t point to any example. Did you forget saving it as a gist?

And the map is only build one if you store it. I updated the code to show that:

If you want your functions to handle variables, I expect them to take different variables. At this point I believe that the suggestion of @gregwtmtno gives you the right amount of power.

You can extend the Hashmap example to functions which take a JSON parameter, but really how many different functions do you have? The way you are trying to do this doesn’t seem to fit a language like rust very well. I believe you should take a step back, look at your original problem again. You might come up with a different solution which is more suited to Rust. Maybe describe your problem here so other people can help you. Post some example data and what you want to happen. This looks a lot like the XY problem.


I haven’t tried it but I suppose it might be possible to load up the DWARF information for the executable and then lookup the function name that way. It might be a bit naughty to do it that way. :stuck_out_tongue:

  • Ehiggs:

I haven’t tried it but I suppose it might be possible to load up the
DWARF information for the executable and then lookup the function name
that way. It might be a bit naughty to do it that way. :stuck_out_tongue:

While that will work (unless the executable has been stripped), it’s easier on ELF systems (such as GNU/Linux) to link with -Bdynamic and use the dlsym function (with an RTLD_DEFAULT argument).


I want to get function name in string and call function with variables
maybe if i can debugging function name in some how


@miko_aab: It looks like you want Remote Procedure Calls.
How about json_rpc ?


@fweimer, even better.

FWIW, I think OP’s question may come from a scripting language bent where you might have a DSL which calls functions. Instead of having to register every gosh darn function and making a mapping up front, it might be preferable to try to look up the symbols in the ELF and if the symbol exists, call it.

If that’s the case, if you’re working with Rust symbols, is it possible to parse the mangled name using a wrapper that yields something like fn get_func(name &str) -> Result<Fn(i32, i32) -> i32>? Using C apis you only get the symbol name and no information about the arguments or return values. And if I understand correctly there is some type erasure that takes place (maybe the terminology is incorrect but I think it’s dune by “coalescing generics”) which make this challenging.


That sound interesting.

I just want to add, that this might pose a security risk - depending on the kind of input. Maybe you don’t want to expose every function of your crate, right? In that case I would suggest some kind of explicit mapping, or at least whitelisting. :slight_smile:

If no arguments are needed, then a simple macro could help to define that mapping

That sounds really interesting. Thx.


If you are looking for something like Ruby’s .send method, you’re unlikely to find it as a built-in feature in compiled languages.

When source code is transformed into executable code, function calls become, essentially, jump statements, which require something like a memory address or a relative memory index.

In interpreted languages like Ruby, this transformation from scoped method name to memory index is (like everything else) done at runtime, so it’s easy to use some kind of string-like data (in Ruby’s case, a symbol) to perform the memory look-up for the destination address corresponding to the start of the desired function.

However, in compiled languages, the runtime typically doesn’t have the infrastructure necessary to perform this lookup on arbitrary strings. There would need to be some kind of lookup table in memory, and a mechanism for actually doing the search and invoking the function. There’s no reason this couldn’t exist, but there would definitely be a speed/memory cost.