Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix String field does not implement `Copy`? [duplicate]

Tags:

rust

borrowing

I am building a simple command-line todo app in Rust. If I don't implement the copy trait I get this error: "move occurs because 'todo' has type 'todo::Todo', which does not implement the 'Copy' trait". When I try to implement the Copy trait for my Todo struct, I receive the following error: "field text: String does not implement the Copy trait". How do I fix this error? My code is below:

pub type todo_type = Vec<Todo>;

#[derive(Copy)]
pub struct Todo {
    id: usize,
    text: String,
    completed: bool,
}

impl Todo {
    pub fn new(text: String, id: usize) -> Todo {
        Todo {
            text,
            id,
            completed: false,
        }
    }
}

pub struct Todos {
    todos: todo_type,
}

impl Todos {
    pub fn new(todos: todo_type) -> Todos {
        Todos { todos }
    }

    pub fn get_all_todos(self) -> todo_type {
        self.todos
    }

    pub fn get_single_todo(self, todo_index: usize) -> Todo {
        unimplemented!()
    }

    pub fn add_todo(self, text: String) -> Todo {
        let id: usize = 1;

        if self.todos.len() == 0 {
            let id = 1;
        } else {
            let last_todo = match self.todos.len() {
                0 => None,
                n => Some(&self.todos[n - 1]),
            };
            let id = last_todo.unwrap().id;
        }

        let todo = Todo::new(text, id);
        self.todos.push(todo);

        todo
    }

    pub fn remove_todo(self, todo_index: usize) -> bool {
        self.todos.remove(todo_index);

        true
    }
}

like image 713
Henry Boisdequin Avatar asked Jan 12 '21 02:01

Henry Boisdequin


People also ask

Why doesn t String implement copy?

String can't implement Copy because (like Vec and any other variable-sized container), it contains a pointer to some variable amount of heap memory. The only correct way to copy a String is to allocate a new block of heap memory to copy all the characters into, which is what String 's Clone implementation does.

Does String implement the copy trait?

A String is a type that does not implement the Copy trait.

What is copy in Rust?

Some types in Rust are very simple. They are called copy types. These simple types are all on the stack, and the compiler knows their size. That means that they are very easy to copy, so the compiler always copies when you send it to a function.


1 Answers

Here you need the Clone trait instead of Copy trait. The Copy trait indicates that the variable can be copied bit-for-bit exactly as is, and that the variables of such type do not underly to move semantics.

Some limitations apply to implementations of Copy trait. It is so, that structs can implement the Copy trait only if none of their components implements the Drop trait. Since String implements the Drop trait, your struct can not implement the Copy.

If you are looking to be making copies of your struct, then you need Clone trait.

like image 147
TheCoolDrop Avatar answered Oct 04 '22 03:10

TheCoolDrop