Move语言社区

【Move进阶】Struct嵌套

address 0x2 {
module StructExample2 {
    use 0x1::STC::STC;
    use 0x1::Vector;
    use 0x1::Token::{Self, Token};

    struct Empty{}

    struct MyStruct {
	address_field: address,
	bool_field: bool,
	u64_field: u64,
        vec_field: vector<u8>,
	inner_field: Empty,
	coins: Token<STC>,
    }

    fun new_empty() : Empty {
	Empty {}
    }

    fun destroy_empty(empty: Empty) {
	let Empty{} = empty;
    }

    public fun new_struct() : MyStruct {
	MyStruct {
	    address_field: 0x1,
            bool_field: true,
            u64_field: 1000000,
            vec_field: Vector::empty(),
            inner_field: Self::new_empty(),
            coins: Token::zero<STC>(),
	}
    }

    public fun destroy_struct(my: MyStruct) {
	let MyStruct {
	    address_field: _,
	    bool_field: _,
            u64_field: _,
            vec_field: _,
            inner_field: empty,
            coins: coins,
	} = my;

	Self::destroy_empty(empty);
	Token::destroy_zero(coins);
    }
}
}