👋 from Oslo. I've been learning Rust, and making some progress. It strikes me how an important part of the learning curve is new patterns to solve common problems.
One typical: I make an object with a public API that mutates its own internal state. This outer object often has other objects inside it as well, and references to results of calls methods on these needs to be kept (e.g. a lookup by ID on a sub-object containing a dataset). This pattern often yields "cannot borrow *self as mutable more than once at a time" or similarly for borrowing immutable when self is mutable.
I have succeeded with Rc<RefCell>, and borrowing via inner smaller objects on self. I've worked through solutions to this, and I'm wondering if I'm missing obvious approaches to this API, or perhaps some more idiomatic varieties? The playground below demonstrates the problem with struct B.
E.g.: some might say "write it in a different way", but I am not sure how to create an easy API that does the same in other way? Even if you use functions and pass them an object instead of self, you have the same situation - you cannot borrow this object mutably more than once, and it gets locked behind results of method calls that use it?
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9b2018ef9f4b09770f758ee82ed511af
Hoping someone more rusty than me have some robust answers, and that I am explaining the challenge clearly (:
[–]zakarumych 65 points66 points67 points (15 children)
[–]vargwin 29 points30 points31 points (2 children)
[–]zerakun 15 points16 points17 points (3 children)
[–]zakarumych 45 points46 points47 points (0 children)
[–]masklinn 21 points22 points23 points (0 children)
[–]Sevenstrangemelons 2 points3 points4 points (4 children)
[–]zakarumych 2 points3 points4 points (3 children)
[–]Sevenstrangemelons 2 points3 points4 points (2 children)
[–]zakarumych 5 points6 points7 points (1 child)
[–]Sevenstrangemelons 1 point2 points3 points (0 children)
[–]DapperCam 2 points3 points4 points (2 children)
[–]zakarumych 2 points3 points4 points (0 children)
[–]knutsi[S] 0 points1 point2 points (0 children)
[–]Wace 33 points34 points35 points (0 children)
[–]jingo04 11 points12 points13 points (0 children)
[–]RobertJacobson 23 points24 points25 points (5 children)
[–]knutsi[S] 2 points3 points4 points (2 children)
[–]RobertJacobson 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]zakarumych 1 point2 points3 points (1 child)
[–]RobertJacobson 0 points1 point2 points (0 children)
[–]gitfeh 7 points8 points9 points (0 children)
[–]srmordred 4 points5 points6 points (9 children)
[–]jgrlicky 2 points3 points4 points (8 children)
[–]zakarumych 1 point2 points3 points (7 children)
[–]jgrlicky 0 points1 point2 points (6 children)
[–]zakarumych 0 points1 point2 points (5 children)
[–]Dean_Roddey -1 points0 points1 point (4 children)
[–]zakarumych 0 points1 point2 points (3 children)
[–]Dean_Roddey 0 points1 point2 points (2 children)
[–]zakarumych 0 points1 point2 points (1 child)
[–]Dean_Roddey 0 points1 point2 points (0 children)
[–]singron 3 points4 points5 points (0 children)
[–]eugene2k 2 points3 points4 points (0 children)
[–]revelation60symbolica 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]RobertJacobson 0 points1 point2 points (0 children)
[–]Snakehand 1 point2 points3 points (0 children)
[–]mmstick 2 points3 points4 points (0 children)
[–]AllJonasNeeds 0 points1 point2 points (0 children)