Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions reference/uses.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ use std::option::some as s;

fun new_vec(): vector<std::option::Option<u8>> {
let mut v = vector[];
vector::push_back(&mut v, s(0));
vector::push_back(&mut v, std::option::none());
push_back(&mut v, s(0));
push_back(&mut v, std::option::none());
v
}
```
Expand Down Expand Up @@ -163,8 +163,8 @@ use std::vector;

fun new_vec(): vector<Option<u8>> {
let mut v = vector[];
vector::push_back(&mut v, 0);
vector::push_back(&mut v, 10);
vector::push_back(&mut v, some(0));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be option::some

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or add a use statement above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relevant use statement is below, which is what this example needs to demonstrate.

vector::push_back(&mut v, none());
v
}

Expand All @@ -183,9 +183,9 @@ You can add `use` declarations to the beginning of any expression block
```move
module a::example;

fun new_vec(): vector<Option<u8>> {
fun new_vec(): vector<std::option::Option<u8>> {
use std::vector::push_back;
use std::option::{Option, some, none};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove Option type import?

@zcy1024 zcy1024 Jul 9, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, use inside a function body is not effective during the function declaration. The previous writing method does not report an error because std::option::Option is imported by default. I made this change to make that clear.

Of course, I just made the changes based on my understanding. If my understanding itself is wrong, please correct me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To illustrate further, I wrote a small example:

module examples::a {
    public struct MyOption<T> has copy, drop, store {
        vec: vector<T>
    }

    public fun some<T>(e: T): MyOption<T> {
        MyOption {
            vec: vector[e]
        }
    }

    public fun none<T>(): MyOption<T> {
        MyOption {
            vec: vector[]
        }
    }
}

module examples::b {
    // If you replace the two commented lines below, you will get an error.
    // fun new_vec(): vector<MyOption<u8>> {
    //     use examples::a::{MyOption, some, none};
    fun new_vec(): vector<examples::a::MyOption<u8>> {
        use examples::a::{some, none};

        let mut v = vector[];
        v.push_back(some(0));
        v.push_back(none());
        v
    }
}

use std::option::{some, none};

let mut v = vector[];
push_back(&mut v, some(0));
Expand All @@ -200,10 +200,10 @@ block.
```move
module a::example;

fun new_vec(): vector<Option<u8>> {
fun new_vec(): vector<std::option::Option<u8>> {
let result = {
use std::vector::push_back;
use std::option::{Option, some, none};
use std::option::{some, none};

let mut v = vector[];
push_back(&mut v, some(0));
Expand All @@ -217,10 +217,10 @@ fun new_vec(): vector<Option<u8>> {
Attempting to use the alias after the block ends will result in an error

```move
fun new_vec(): vector<Option<u8>> {
fun new_vec(): vector<std::option::Option<u8>> {
let mut result = {
use std::vector::push_back;
use std::option::{Option, some, none};
use std::option::{some, none};

let mut v = vector[];
push_back(&mut v, some(0));
Expand Down