pub fn astar_path<T>(
    start_node: T,
    nodes: HashMap<T, (Vec<(T, f32)>, f32)>,
    end_node: T
) -> Option<Vec<T>> where
    T: Eq + Hash + Debug + Clone + Copy
Expand description

Will find the most optimal path from start_node to end_node if it exists. The nodes data set uses the keys as labels to uniquely identify a node/travel point. The values take the form of a tuple containing:

  • Vector of tuples: (neighbour_label, distance_to_neighbour) - used to explore possible paths to traverse
  • Weight - the heuristic which helps judge whether a given route is good or bad

For instance:

use std::collections::HashMap;
use pathfinding_astar::astar_path;

let start: i32 = 0;
let end: i32 = 2;
let mut nodes: HashMap<i32, (Vec<(i32, f32)>, f32)> = HashMap::new();
nodes.insert(0, (vec![(1, 5.0)], 3.0));
nodes.insert(1, (vec![(0, 5.0), (2, 4.0)], 2.0));
nodes.insert(2, (vec![(1, 4.0)], 6.0));
let path = astar_path(start, nodes, end).unwrap();
assert_eq!(vec![0, 1, 2], path);