我正在尝试编写一个函数rotate_card
,该函数接受矢量作为输入,将矢量的前元素旋转到后部,并返回一个包含旋转元素和旋转产生的矢量的对。 / p>
#[derive(Debug)]
enum Card {
Ace,
King,
Queen,
Jack,
}
type Deck = Vec<Card>;
fn rotate_card(deck: &mut Deck) -> (Card, &mut Deck) {
let top_card = deck.remove(0);
deck.push(top_card);
(top_card, deck)
} // end rotate_card
fn main() {
let mut my_deck: Deck = vec![Card::Ace, Card::King, Card::Queen, Card::Jack];
let z: (Card, &mut Deck) = rotate_card(&mut my_deck);
println!("The value of z is: {:?}.", z);
} // end main
error[E0382]: use of moved value: `top_card`
--> src/main.rs:14:6
|
13 | deck.push(top_card);
| -------- value moved here
14 | (top_card, deck)
| ^^^^^^^^ value used here after move
|
= note: move occurs because `top_card` has type `Card`, which does not implement the `Copy` trait
如何解决value used after move
错误?
答案 0 :(得分:6)
我该如何解决
您不会“解决”此类问题。所有权是Rust中的一个基本概念,您必须了解它。
Card
隐式复制您的Copy
#[derive(Debug, Copy, Clone)]
enum Card { /* ... */ }
Card
使您的Clone
明确可复制#[derive(Debug, Clone)]
enum Card { /* ... */ }
fn rotate_card(deck: &mut Deck) -> Card {
let top_card = deck.remove(0);
deck.push(top_card.clone());
top_card
}
您可以返回对最后一张卡的引用,而不是作为值的卡:
fn rotate_card(deck: &mut Deck) -> &mut Card {
let top_card = deck.remove(0);
deck.push(top_card);
deck.last_mut().unwrap()
}
use std::rc::Rc;
type Deck = Vec<Rc<Card>>;
fn rotate_card(deck: &mut Deck) -> Rc<Card> {
let top_card = deck.remove(0);
deck.push(top_card.clone());
top_card
}
这是一个无用的函数签名:
fn rotate_card(deck: &mut Deck) -> (Card, &mut Deck)
没有理由将Deck
返回给呼叫者;他们已经已经有了该参考。删除它。
切片(和Vec
通过DerefMut
)具有rotate_left
方法;您无需重新实现它。