Skip to main content
added 53 characters in body
Source Link
L. F.
  • 9.6k
  • 2
  • 26
  • 68

Many of your methods can be simplified (?) with iterators. Here's a few examples:


fn get_distance(&self, vertex: Vertex) -> u32 {
    let mut ret = 0;

    for r in &self.roads {
        if r.vertex == vertex {
            ret = r.distance;
        }
    }

    ret
}

becomes

fn get_distance(&self, vertex: Vertex) -> u32 {
    self.roads
        .iter()
        .rev()
        .find(|road| road.vertex == vertex)
        .map(|road| road.distance)
        .unwrap_or(0)
}

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    for r in &mut self.roads {
        if r.vertex == *vertex {
            return Some(r);
        }
    }

    None
}

becomes

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    self.roads
        .iter_mut()
        .find(|road| road.vertex == vertex)
}

fn remove(&mut self, v : &Vertex) {
    let mut index = 0;
    while index < self.unvisited.len() {
        let toremove = &self.unvisited[index];
        if v == toremove {
            self.unvisited.remove(index);
            break
        }
        index += 1;
    }
}

becomes

fn remove(&mut self, v: &Vertex) {
    let index = self.unvisited.iter().position(|vertex| vertex == v);
    if let Some(index) = index {
        self.unvisited.remove(index);
    }
}

Many of your methods can be simplified (?) with iterators. Here's a few examples:


fn get_distance(&self, vertex: Vertex) -> u32 {
    let mut ret = 0;

    for r in &self.roads {
        if r.vertex == vertex {
            ret = r.distance;
        }
    }

    ret
}

becomes

fn get_distance(&self, vertex: Vertex) -> u32 {
    self.roads
        .iter()
        .rev()
        .find(|road| road.vertex == vertex)
        .map(|road| road.distance)
        .unwrap_or(0)
}

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    for r in &mut self.roads {
        if r.vertex == *vertex {
            return Some(r);
        }
    }

    None
}

becomes

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    self.roads
        .iter_mut()
        .find(|road| road.vertex == vertex)
}

fn remove(&mut self, v : &Vertex) {
    let mut index = 0;
    while index < self.unvisited.len() {
        let toremove = &self.unvisited[index];
        if v == toremove {
            self.unvisited.remove(index);
            break
        }
        index += 1;
    }
}

becomes

fn remove(&mut self, v: &Vertex) {
    let index = self.unvisited.iter().position(|vertex| vertex == v);
    self.unvisited.remove(index);
}

Many of your methods can be simplified (?) with iterators. Here's a few examples:


fn get_distance(&self, vertex: Vertex) -> u32 {
    let mut ret = 0;

    for r in &self.roads {
        if r.vertex == vertex {
            ret = r.distance;
        }
    }

    ret
}

becomes

fn get_distance(&self, vertex: Vertex) -> u32 {
    self.roads
        .iter()
        .rev()
        .find(|road| road.vertex == vertex)
        .map(|road| road.distance)
        .unwrap_or(0)
}

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    for r in &mut self.roads {
        if r.vertex == *vertex {
            return Some(r);
        }
    }

    None
}

becomes

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    self.roads
        .iter_mut()
        .find(|road| road.vertex == vertex)
}

fn remove(&mut self, v : &Vertex) {
    let mut index = 0;
    while index < self.unvisited.len() {
        let toremove = &self.unvisited[index];
        if v == toremove {
            self.unvisited.remove(index);
            break
        }
        index += 1;
    }
}

becomes

fn remove(&mut self, v: &Vertex) {
    let index = self.unvisited.iter().position(|vertex| vertex == v);
    if let Some(index) = index {
        self.unvisited.remove(index);
    }
}
Source Link
L. F.
  • 9.6k
  • 2
  • 26
  • 68

Many of your methods can be simplified (?) with iterators. Here's a few examples:


fn get_distance(&self, vertex: Vertex) -> u32 {
    let mut ret = 0;

    for r in &self.roads {
        if r.vertex == vertex {
            ret = r.distance;
        }
    }

    ret
}

becomes

fn get_distance(&self, vertex: Vertex) -> u32 {
    self.roads
        .iter()
        .rev()
        .find(|road| road.vertex == vertex)
        .map(|road| road.distance)
        .unwrap_or(0)
}

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    for r in &mut self.roads {
        if r.vertex == *vertex {
            return Some(r);
        }
    }

    None
}

becomes

fn get_road_mut(&mut self, vertex: &Vertex) -> Option<&mut Road> {
    self.roads
        .iter_mut()
        .find(|road| road.vertex == vertex)
}

fn remove(&mut self, v : &Vertex) {
    let mut index = 0;
    while index < self.unvisited.len() {
        let toremove = &self.unvisited[index];
        if v == toremove {
            self.unvisited.remove(index);
            break
        }
        index += 1;
    }
}

becomes

fn remove(&mut self, v: &Vertex) {
    let index = self.unvisited.iter().position(|vertex| vertex == v);
    self.unvisited.remove(index);
}