Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link

Icon LinkString

The Rust SDK represents Fuel's Strings as SizedAsciiString<LEN>, where the generic parameter LEN is the length of a given string. This abstraction is necessary because all strings in Fuel and Sway are statically-sized, i.e., you must know the size of the string beforehand.

Here's how you can create a simple string using SizedAsciiString:

let ascii_data = "abc".to_string();
 
SizedAsciiString::<3>::new(ascii_data)
    .expect("Should have succeeded since we gave ascii data of correct length!");

To make working with SizedAsciiStrings easier, you can use try_into() to convert from Rust's String to SizedAsciiString, and you can use into() to convert from SizedAsciiString to Rust's String. Here are a few examples:

    #[test]
    fn can_be_constructed_from_str_ref() {
let _: SizedAsciiString<3> = "abc".try_into().expect("Should have succeeded");
    }
 
    #[test]
    fn can_be_constructed_from_string() {
let _: SizedAsciiString<3> = "abc".to_string().try_into().expect("Should have succeeded");
    }
 
    #[test]
    fn can_be_converted_into_string() {
let sized_str = SizedAsciiString::<3>::new("abc".to_string()).unwrap();
 
let str: String = sized_str.into();
 
assert_eq!(str, "abc");
    }

If your contract's method takes and returns, for instance, a Sway's str[23]. When using the SDK, this method will take and return a SizedAsciiString<23>, and you can pass a string to it like this:

    let call_handler = contract_instance.methods().takes_string(
"This is a full sentence"
    .try_into()
    .expect("failed to convert string into SizedAsciiString"),
    );