用墨水!你可以直接使用String
类型。
这是一个简单的墨水实现!通过测试执行此操作的合同(从incrementer
示例修改:
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract(version = "0.1.0")]
mod basic_string {
#[ink(storage)]
struct BasicString {
value: String,
}
impl BasicString {
#[ink(constructor)]
fn new(init_value: String) -> Self {
Self { value: init_value }
}
#[ink(constructor)]
fn default() -> Self {
Self::new(Default::default())
}
#[ink(message)]
fn set(&mut self, new: String) {
self.value = new;
}
#[ink(message)]
fn get(&self) -> String {
self.value.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_works() {
let contract = BasicString::default();
assert_eq!(contract.get(), "");
}
#[test]
fn it_works() {
let mut contract = BasicString::new("Hello World!".into());
assert_eq!(contract.get(), "Hello World!");
contract.set("Goodbye!".into());
assert_eq!(contract.get(), "Goodbye!");
}
}
}