Hello,
Today I have encountered following problem. I am given an csv file and for each of it's elements I would like to initialize my struct:
#[derive(Debug)]
enum Directions {
RIGHT,
LEFT,
UP,
DOWN,
}
#[derive(Debug)]
struct Move {
pub direction: Directions,
pub value: i32,
}
Code which is supposed to do that looks like that:
pub fn draw_wire(wire_moves: &str) -> () {
let moves: Vec<Move> = wire_moves
.split(",")
.into_iter()
.map(|move_elem|
Move {
direction: match move_elem[..0] {
"R" => Directions::RIGHT,
"L" => Directions::LEFT,
"U" => Directions::UP,
"D" => Directions::DOWN,
_ => panic!("Invalid move direction!")
},
value: move_elem[1..].parse::<i32>().unwrap()
}
)
.collect();
println!("{:?}", moves);
}
So basically, what I am trying to do here is to return correct enum value based on first character of each element. The problem is, that currently I am getting following error:
error[E0308]: mismatched types
--> src/main.rs:39:28
|
39 | direction: match move_elem[..0] {
| ^^^^^^^^^^^^^^ expected &str, found str
|
= note: expected type `&str`
found type `str`
Could you please help me with that? What is the real problem here?
Regards!