Skip to content
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

Moved value is still accessible #24069

Closed
mdinger opened this issue Apr 4, 2015 · 5 comments
Closed

Moved value is still accessible #24069

mdinger opened this issue Apr 4, 2015 · 5 comments

Comments

@mdinger
Copy link
Contributor

mdinger commented Apr 4, 2015

This looks like a bug to me. No one responded on IRC so I'm uncertain though:

#[derive(Debug)]
struct S {
    val: Option<Box<S>>,
}

fn main() {
    let mut a = S { val: None };
    let _ = S { val: Some(Box::new(a)) };

    // `a` was just moved. I can still assign to it though.
    // kinda seems like a bug.
    a.val = None;

    // assignment works but println fails because of move. kinda weird.
    //println!("{:?}", a.val);
}

playpen

@steveklabnik
Copy link
Member

I don't think this is a bug. Moved values aren't usable, but you're not using it, you're assigning a new value to it.

@steveklabnik
Copy link
Member

/cc @nikomatsakis

@mdinger
Copy link
Contributor Author

mdinger commented Apr 4, 2015

This can be misleading. A guy was showing this on IRC. If it had triggered a move error he probably would have caught it. As it is, he wasn't noticing that the value had moved. The original question on botbot.


The original questions code:

#[derive(Debug)]
pub struct Node<T>{
    pub item: T,
    pub next: Option<Box<Node<T>>>
}

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

pub fn main() {
    let mut first = Node::new("to");
    let mut second = Node::new("be");
    let mut third = Node::new("or");
    let mut fourth = Node::new("not");
    let fifth = Node::new("to");

    first.next = Some(Box::new(second));
    second.next = Some(Box::new(third));
    third.next = Some(Box::new(fourth));
    fourth.next = Some(Box::new(fifth));

    println!("{:?}", first);
}

@sfackler
Copy link
Member

sfackler commented Apr 4, 2015

I believe this is a dup of #21232

@steveklabnik
Copy link
Member

Ah yes it is

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

No branches or pull requests

3 participants