> For the complete documentation index, see [llms.txt](https://move-developers-dao.gitbook.io/aptos-move-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://move-developers-dao.gitbook.io/aptos-move-by-example/applications/collection.md).

# Collection

* Basic contract is created for storing `Item` on `Collection` struct in vector form.
* Some of the function like  `start_collection`, `exists_at`, `add_item`, `size` and `destroy` are called publicly in the contract.
* Scripts are also written for this contract.

```rust
module 0x12::Collection{
    use std::vector;
    use std::signer;

    struct Item has store, drop{}

    struct Collection has store, key{
        items:vector<Item>
    }

    public fun start_collection(account: &signer){
        move_to<Collection>(account,Collection{
            items: vector::empty<Item>()
        })
    }

    public fun exists_at(at: address): bool {
        exists<Collection>(at)
    }

    public fun add_item(account: &signer) acquires Collection{
        let addr = signer::address_of(account);
        let collection = borrow_global_mut<Collection>(addr);
        vector::push_back(&mut collection.items,Item{});
    }

    public fun size(account: &signer):u64 acquires Collection{
        let addr = signer::address_of(account);
        let collection = borrow_global_mut<Collection>(addr);
        vector::length(& collection.items)
    }

    public fun destroy(account: &signer) acquires Collection{
        let addr = signer::address_of(account);
        let collection = move_from<Collection>(addr);

        let Collection{items: _} = collection;
    }
}
```

### Script of Collection contract:

```rust
script{
    use 0x12::collection as coll;
    use std::debug;
    use std::signer;

    fun main_resource(account: signer){
        let addr = signer::address_of(&account);
        // OR
        // let addr = @0x63;
        
        coll::destroy(&account);
        coll::start_collection(&account);
        let ea = coll::exists_at(addr);
        debug::print(&ea);
        coll::add_item(&account);
        coll::add_item(&account);
        coll::add_item(&account);
        let lsize = coll::size(&account);
        debug::print(&lsize);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://move-developers-dao.gitbook.io/aptos-move-by-example/applications/collection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
