logo
down
shadow

Cannot move out of dereference of `&mut`-pointer when calling Option::unwrap


Cannot move out of dereference of `&mut`-pointer when calling Option::unwrap

By : Drazen J
Date : November 14 2020, 04:48 PM
wish helps you The Rust compiler makes no pretence to intellectual eminence or scholarship sublime; it does exactly what you tell it to, not what you meant or wanted.
In this case, when you say &mut root.left.unwrap(), what you are wanting is something that takes Option> by mutable reference and gives you a &mut Box; but that’s definitely not what you actually told it to do.
code :
fn unwrap(self) -> T
fn init_tree(root: &mut Node, n: int) {
    if n != 0 {
        root.left = Some(box Node::new());
        root.right = Some(box Node::new());
        init_tree(match root.left { Some(box ref mut x) => x, None => unreachable!() }, n - 1);
        init_tree(match root.right { Some(box ref mut x) => x, None => unreachable!() }, n - 1);
    }
}
fn init_tree(root: &mut Node, n: int) {
    if n != 0 {
        let mut left = box Node::new();
        let mut right = box Node::new();
        init_tree(&mut *left, n - 1);
        init_tree(&mut *right, n - 1);
        root.left = Some(left);
        root.right = Some(right);
    }
}


Share : facebook icon twitter icon
String append, cannot move out of dereference of '&'pointer

String append, cannot move out of dereference of '&'pointer


By : user2155439
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm having trouble combining two strings, I'm very new to rust so If there is an easier way to do this please feel free to show me. , The function definition of append is:
code :
fn append(self, second: &str) -> String
let string_pairs = vec![("Foo".to_string(),"Bar".to_string())];

// Option 1: leave original vector intact
let mut strings = Vec::new();
for &(ref x, ref y) in string_pairs.iter() {
    let string = x.clone().append(y.as_slice());
    strings.push(string);
}

// Option 2: consume original vector
let strings: Vec<String> = string_pairs.move_iter()
    .map(|(x, y)| x.append(y.as_slice()))
    .collect();
Rust cannot move out of dereference pointer

Rust cannot move out of dereference pointer


By : Arockia Hilton
Date : March 29 2020, 07:55 AM
Does that help Your problem is that get_right() accepts Option>, while it should really accept Option<&Node> and return Option<&Node> as well. The call site should be also changed appropriately.
Here is the explanation. Box is a heap-allocated box. It obeys value semantics (that is, it behaves like plain T except that it has associated destructor so it is always moved, never copied). Hence passing just Box into a function means giving up ownership of the value and moving it into the function. However, it is not what you really want and neither can do here. get_right() function only queries the existing structure, so it does not need ownership. And if ownership is not needed, then references are the answer. Moreover, it is just impossible to move the self.min into a function, because self.min is accessed through self, which is a borrowed pointer. However, you can't move out from a borrowed data, it is one of the basic safety guarantees provided by the compiler.
code :
fn get_right(e: Option<&Node>) -> Option<&Node> {
    e.and_then(|n| n.right.as_ref().map(|r| &**r))
}
println!("{}", get_right(self.min.map(|r| &**r))
fn and_then<U>(self, f: |T| -> Option<U>) -> Option<U> {
    match self {
        Some(e) => f(e),
        None => None
    }
}
Cannot move out of dereference of `&`-pointer

Cannot move out of dereference of `&`-pointer


By : Jan Bolek
Date : March 29 2020, 07:55 AM
Hope this helps self.declarations.clone() clones the Arc, while I believe you intended to clone the Vec. To resolve the call to map_in_place, the compiler automatically dereferences the Arc to be able to call the method, which is defined on Vec. The compiler knows how to dereference the Arc because Arc implements Deref. deref returns a borrowed pointer, and this is where the error comes from. To clone the Vec, we must explicitly dereference the Arc: (*self.declarations).clone().
However, map_in_place is not suitable in this case, as (PropertyDeclaration, PropertyDeclarationImportance) doesn't have the same size as PropertyDeclaration (unless PropertyDeclarationImportance has a size of zero, which is probably not the case), which is required per the docs. It fails with a message similar to this:
code :
task '<main>' failed at 'assertion failed: mem::size_of::<T>() == mem::size_of::<U>()', /build/rust-git/src/rust/src/libcollections/vec.rs:1805
impl PropertyDeclarationBlock {
    pub fn select_declarations(&self) -> Arc<Vec<PropertyDeclaration>> {
        Arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect())
    }
}
Why should I dereference a pointer before calling the operator() in C++?

Why should I dereference a pointer before calling the operator() in C++?


By : user2779091
Date : March 29 2020, 07:55 AM
this one helps.
Why do I have to convert the object to a function pointer before calling operator()
code :
(*this->distribution)
distribution->operator()(randomNumGen);
`cannot move out of dereference of `&mut`-pointer` while building a sorted linked list

`cannot move out of dereference of `&mut`-pointer` while building a sorted linked list


By : user2965843
Date : March 29 2020, 07:55 AM
hop of those help? Here's a similar example:
code :
enum E { Hello }
struct A(E);

fn main() {
    let mut a = A(E::Hello);
    let b = &mut a;
    let c = b.0;
}
<anon>:7:13: 7:14 error: cannot move out of dereference of `&mut`-pointer
<anon>:7     let c = b.0;
                     ^
<anon>:7:9: 7:10 note: attempting to move value to here
<anon>:7     let c = b.0;
                 ^
<anon>:7:9: 7:10 help: to prevent the move, use `ref c` or `ref mut c` to capture value by reference
<anon>:7     let c = b.0;
                 ^
#[derive(Debug)]
struct Node<T> {
    v: T,
    next: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(v: T) -> Node<T> { Node { v: v, next: None } }

    fn push_front(self, head: T) -> Node<T> {
        Node {
            v: head,
            next: Some(Box::new(self)),
        }
    }

    fn push_back(&mut self, tail: T) {
        match self.next {
            Some(ref mut next) => next.push_back(tail), 
            None => self.next = Some(Box::new(Node::new(tail))),
        }
    }

    fn push_after(&mut self, v: T) {
        let old_next = self.next.take();

        let new_next = Node {
            v: v,
            next: old_next,
        };

        self.next = Some(Box::new(new_next));
    }
}

fn main() {
    let mut n = Node::new(2u8);
    n.push_back(3u8);
    let mut n = n.push_front(0u8);
    n.push_after(1u8);

    println!("{:?}", n);
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org