를 downcasting 후 여러 값을 열거 일치하는 패턴 :사용 때 한 <code>String</code> 매개 변수가있는 <code>enum</code>에 일치하는 패턴을 사용할 수 있습니다
extern crate robots;
use std::any::Any;
use robots::actors::{Actor, ActorCell};
#[derive(Clone, PartialEq)]
pub enum ExampleMessage {
Msg { param_a: String },
}
pub struct Dummy {}
impl Actor for Dummy {
// Using `Any` is required for actors in RobotS
fn receive(&self, message: Box<Any>, _context: ActorCell) {
if let Ok(message) = Box::<Any>::downcast::<ExampleMessage>(message) {
match *message {
ExampleMessage::Msg { param_a } => println!("got message"),
}
}
}
}
그리고 아직 내가 열거 패턴 매칭을 수행 할 수 없습니다 오전
#[derive(Clone, PartialEq)]
pub enum ExampleMessage {
Msg { param_a: String, param_b: usize },
}
impl Actor for Dummy {
// Using `Any` is required for actors in RobotS
fn receive(&self, message: Box<Any>, _context: ActorCell) {
if let Ok(message) = Box::<Any>::downcast::<ExampleMessage>(message) {
match *message {
ExampleMessage::Msg { param_a, param_b } => println!("got message"),
}
}
}
}
이 오류가 발생합니다 :
error[E0382]: use of moved value: `message`
--> src/example.rs:19:48
|
19 | ExampleMessage::Msg { param_a, param_b } => {
| ------- ^^^^^^^ value used here after move
| |
| value moved here
|
= note: move occurs because `message.param_a` has type `std::string::String`, which does not implement the `Copy` trait
012,351,641 2 개 매개 변수
이전에 다운 캐스팅하지 않고 동일한에 패턴 일치를 시도했는데 정상적으로 작동하지만 다운 캐스트가 필요합니다. 이것은 매우 이상한 행동으로 보입니다. 나는이 오류를 우회하는 법을 모릅니다.
내가 (2017년 4월 29일 afa1240e5) 녹 1.19.0 - 야간 사용하고
내 정신 모형을 수정하는 데 아주 힘든 시간을 보내고 있습니다 ... 원래 버전의'receive2 '는 무엇이 잘못 되었습니까? 왜 단일 struct 필드 만 바인딩 할 때 문제가 사라지나요? 'Box <> '가 특별하기 때문에 뭔가 관련이 있습니까? 또한 - 어떤 특별한 힘을 말하는 겁니까? Thanks Shep – user1935361
@ user1935361 조금 더 많은 텍스트를 추가했습니다. – Shepmaster
[1]에 대해서 - 왜냐하면'Deref'는 항상 참조를 반환하기 때문에 나는 그것을 얻었을 것이라고 생각합니다. – user1935361