Created
October 25, 2017 19:27
-
-
Save luki/4a725eeb3169cabc2514e92b0cc7fe7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
trait StringToF32 { | |
fn to_f32(self) -> f32; | |
} | |
impl StringToF32 for String { | |
fn to_f32(self) -> f32 { | |
self.parse::<f32>().unwrap() | |
} | |
} | |
struct Coordinate { | |
x: f32, | |
y: f32, | |
z: f32 | |
} | |
impl Coordinate { | |
fn newFromPointStr(r: &Vec<String>) -> Option<Coordinate> { | |
if r.len() != 4 { return None } | |
Some(Coordinate { | |
x: r[1].clone().to_f32(), | |
y: r[2].clone().to_f32(), | |
z: r[3].clone().to_f32() | |
}) | |
} | |
} | |
fn main() { | |
let mut place_coordinates: HashMap<String, Coordinate> = HashMap::new(); | |
let coordinates_str = "Luke,3,2,3\nLea,5,4,8\nBelana,1,3,10"; // Name,x,y,z | |
for line in coordinates_str.lines() { | |
let elements = line.split(",").map(|x|{x.to_string()}).collect::<Vec<String>>(); | |
if elements.len() == 4 { | |
if let Some(c) = Coordinate::newFromPointStr(&elements) { | |
place_coordinates.insert(elements[0].clone().to_string(), c); | |
} | |
} | |
} | |
match place_coordinates.get("Belana") { | |
Some(p) => println!("{}", p.x), | |
None => println!(":/") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment