Skip to content

Mutation, references, and borrowing documentation #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

apribiso
Copy link
Collaborator

In the main document, I succinctly describe ownership, references, and borrowing in Rust, while also linking the official Rust documentation that contains more examples. Then, I shortly explain how to use mutable references in requires, ensures, and assert, while providing simple and more complex examples.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copy link
Collaborator

@parno parno left a comment

Choose a reason for hiding this comment

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

Thanks very much for writing this up! I added a few minor comments.

{{#include ../../../../examples/guide/references.rs:immut}}
```

Verus only support the use of mutable references as arguments to a function, such as in the following example.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"only support" -> "only supports" and perhaps "Verus only" -> "Currently Verus only"

{{#include ../../../../examples/guide/references.rs:mut}}
```

Given that the pieces of memory to which mutable references point can be modified during function calls, we need a particular way to talk about their old and updated in Verus spec code, particularly in the `requires` and `ensures` of functions and in `assert`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might phrase this as:

about their old and updated values in Verus spec code, particularly in function requires and ensures clauses, and in assert statements.

// ANCHOR_END: immut
}

// ANCHOR: mut
Copy link
Collaborator

Choose a reason for hiding this comment

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

Stylistically, we generally sort functions such that callees precede callers, so in this case, that would mean putting modify_y before mutable_example.

*a = *a + 1;
assert(*a == 1);
*a = *a + 1;
assert(*a == 2);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could add another copy of assert(*old(a) == 0); in here to emphasize that old refers to the value at the very beginning of the function, not to the value right before the last update.

@apribiso
Copy link
Collaborator Author

Implemented suggestions :)

@utaal utaal self-assigned this May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants