StdString
A dynamic string of variable length can be represented using the StdString
type, also known as a Standard Lib String or std-lib-string
. It behaves much like a dynamic string in most languages, and is essentially an array of characters.
// #import { StdString };
const stdString: StdString = 'Hello World';
const { value } = await contract.functions
.string_comparison(stdString)
.txParams({ gasLimit: 10_000 })
.simulate();
expect(value).toBeTruthy();
StdString
The StdString
type can be integrated with your contract calls. Consider the following contract that can compare and return a String:
contract;
use std::string::String;
abi StdStringTest {
fn echo_string(value: String) -> String;
fn string_comparison(value: String) -> bool;
}
impl StdStringTest for Contract {
fn echo_string(value: String) -> String {
value
}
fn string_comparison(value: String) -> bool {
let expected = String::from_ascii_str("Hello World");
value.as_bytes() == expected.as_bytes()
}
}
A string can be created using a native JavaScript string, and sent to a Sway contract:
// #import { StdString };
const stdString: StdString = 'Hello Fuel';
const { value } = await contract.functions
.echo_string(stdString)
.txParams({ gasLimit: 10_000 })
.simulate();
expect(value).toEqual(stdString);