Skip to main content

字符串

在 Move 中,没有原生的字符串类型,但有一个有用的装饰器。

module examples::strings {
use sui::object::{Self, UID};
use sui::tx_context::TxContext;

// Use this dependency to get a type wrapper for UTF-8 strings
use std::string::{Self, String};

/// A dummy Object that holds a String type
struct Name has key, store {
id: UID,

/// Here it is - the String type
name: String
}

/// Create a name Object by passing raw bytes
public fun issue_name_nft(
name_bytes: vector<u8>, ctx: &mut TxContext
): Name {
Name {
id: object::new(ctx),
name: string::utf8(name_bytes)
}
}
}

字符串字面量

在 Move 中,通过将字符串前置 b 来将其定义为字节字符串。

const IMAGE_URL: vector<u8> = b"https://api.capy.art/capys/";

let keys = vector[
utf8(b"name"),
utf8(b"link"),
utf8(b"image_url"),
utf8(b"description"),
utf8(b"project_url"),
utf8(b"creator"),
];