Converting between types (u8, u64, b256, str, String, Vec) in Sway

If you are converting between types in Sway, this is the guide for you:
https://docs.fuel.network/docs/nightly/sway/basics/converting_types/

u8, u64, b256, string etc.

This is coming up in nightly, and highlights all the different conversions you might typically run into in Sway.

If you have others you think might be helpful for other devs that are not in this doc, please post the code below.

4 Likes

Thanks.

I’m still confused how to convert “str” to “String”

This should be easy, I do the following in my SRC20 implementation:
First I have the str[]:

configurable {
    /// The name of a specific asset minted by this contract.
    name: str[5] = __to_str_array("Token"),
}

and then I do:

    fn name(asset: AssetId) -> Option<String> {
        if asset == AssetId::default() {
            Some(String::from_ascii_str(from_str_array(name)))
        } else {
            None
        }
    }