Home Updating structs
Post
Cancel

Updating structs

Do you remember how, in Cairo 0, there was no concise way how to update a struct? We had to write out all its members even if only one changed. It was pretty annoying, as RoboTeddy pointed out in his now legendary post.

Cairo 1 fixes that. By declaring a struct as mutable it becomes possible to assign new values to any member of said struct:

1
2
3
4
5
6
7
8
9
struct Point {
    x: u64,
    y: u64,
    z: u64
}

let mut place = Point { x: 0, y: 42, z: 69 };

place.x = 33; // Point { x: 33, y: 42, z: 69 }

Easy and kinda obvious, isn’t it? h/t to bllu for teaching me this.

This post is licensed under CC BY 4.0 by the author.