코딩Coding/★Rust★기초★Syntax

Using unsafe 관련 글(The Rustonomicon=unsafe설명서Rust) -Reddit-learnrust

내인생PLUS 2022. 2. 24. 18:33
728x90

https://www.reddit.com/r/learnrust/comments/sistjp/using_unsafe_to_borrow_self_as_mutable_more_than/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

Using unsafe to borrow *self as mutable more than once?

So I have a the following situation, where I am calling two methods inside a struct that borrow self as mutable if let Some(list_item) =...

www.reddit.com



Using unsafe to borrow *self as mutable more than once?

So I have a the following situation, where I am calling two methods inside a struct that borrow self as mutable

    if let Some(list_item) = self.selected_item_mut(){
                  if let Some(item) = self.get_item_in_tree_mut(list_item){
                     <code snipped>

Then I get the "cannot borrow `*self` as mutable more than once at a time".

So then I thought I'd try some unsafe code?

    unsafe{
       if let Some(list_item) = self.selected_item_mut(){
              if let Some(item) = self.get_item_in_tree_mut(list_item){
                 <code snipped>

     }

But then I get the same error along with "unnecessary unsafe block"

Any help appreciated!

edit: I guess what I'm really trying to do is access two different data structures inside the struct, but I can't borrow self as both immutable and mutable, and can't borrow it as mutable twice, and I need a mutable reference to the object inside the second data structure.

Rust
Unsafe 지침서
Nomicon


https://doc.rust-lang.org/nomicon/index.html

Introduction - The Rustonomicon

Warning: This book is incomplete. Documenting everything and rewriting outdated parts take a while. See the issue tracker to check what's missing/outdated, and if there are any mistakes or ideas that haven't been reported, feel free to open a new issue the

doc.rust-lang.org






반응형