1

I am using a macro hex! that accepts only string literals. I have a value returned from a function and stored in a variable. I cannot hard-code the values and call this function. So, how can I call the hex! macro with a variable?

This is my working code:

let account: AccountId32 = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into();

and this is the code where I am facing the error:

let account_id = "d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d";
let account: AccountId32 = hex_literal::hex!(&account_id).into();

The error is:

error: proc-macro derive panicked
  --> src/examples/example_substratekitties.rs:49:32
   |
49 |     let account: AccountId32 = hex_literal::hex!(&account_id).into();
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: message: expected one string literal
   = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

All the examples of the hex! macro only demonstrate it with string literals.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81

1 Answers1

7

You cannot. It is literally impossible. A literal is something where the value is written verbatim in the source code of the program.

Don't use that macro because it doesn't do what you want (emphasis mine):

This crate provides hex! macro for converting hexadecimal string literal to byte array at compile time.

Perhaps you want the hex crate?

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366