我正在Rust中构建一个简单的命令行todo应用程序.如果我没有实现复制特性,我会得到以下错误:"移动是因为'todo'的类型为'todo::todo',而该类型没有实现'copy'特性.".当我try 为Todo struct 实现复制特性时,我收到以下错误:"字段文本:字符串未实现复制特性".如何修复此错误?我的代码如下:
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
}
}