0

In the below code, I'm passing something of u32 type. How can I pass a text(String) instead of u32?

pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
            
            let who = ensure_signed(origin)?;

            // Update storage.
            Something::put(something);

            // Emit an event.
            Self::deposit_event(RawEvent::SomethingStored(something, who));
            // Return a successful DispatchResult
            Ok(())
        }

1 Answer 1

1

String is a special Vec<u8> actually.

You could use Vec<u8>.


Actually, we can use String.

extern crate alloc;

use alloc::string::String;

But we don't do that. It makes things complicated.

As I said, String is a Vec<u8>. You could do anything with Vec<u8>.

b"hello world".to_vec() is also a Vec<u8>.

There is no println! in no_std. So, you don't need the Display.

If you want to log something you could use #[cfg(features = "std")], then you can use String.

For events, you could let apps decode the Vec<u8> to String. That's not our job.

IMO, Vec<u8> is more efficient.

3
  • U know any reason, Why we can't use String? Commented Sep 27, 2022 at 9:50
  • @Samray Because Substrate uses no_std so there's no String type. Commented Sep 27, 2022 at 10:03
  • I updated my anwser.
    – AurevoirXavier
    Commented Sep 27, 2022 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.