Skip to content

Commit 6b3a3f2

Browse files
committed
Auto merge of #30369 - jethrogb:patch-2, r=steveklabnik
The old code returned `usize::MAX` as an error condition, which is not the Rust way.
2 parents 110df04 + 6e46a0f commit 6b3a3f2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/libcollections/binary_heap.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
//! // to each node. This implementation isn't memory-efficient as it may leave duplicate
6767
//! // nodes in the queue. It also uses `usize::MAX` as a sentinel value,
6868
//! // for a simpler implementation.
69-
//! fn shortest_path(adj_list: &Vec<Vec<Edge>>, start: usize, goal: usize) -> usize {
69+
//! fn shortest_path(adj_list: &Vec<Vec<Edge>>, start: usize, goal: usize) -> Option<usize> {
7070
//! // dist[node] = current shortest distance from `start` to `node`
7171
//! let mut dist: Vec<_> = (0..adj_list.len()).map(|_| usize::MAX).collect();
7272
//!
@@ -79,7 +79,7 @@
7979
//! // Examine the frontier with lower cost nodes first (min-heap)
8080
//! while let Some(State { cost, position }) = heap.pop() {
8181
//! // Alternatively we could have continued to find all shortest paths
82-
//! if position == goal { return cost; }
82+
//! if position == goal { return Some(cost); }
8383
//!
8484
//! // Important as we may have already found a better way
8585
//! if cost > dist[position] { continue; }
@@ -99,7 +99,7 @@
9999
//! }
100100
//!
101101
//! // Goal not reachable
102-
//! usize::MAX
102+
//! None
103103
//! }
104104
//!
105105
//! fn main() {
@@ -140,11 +140,11 @@
140140
//! // Node 4
141141
//! vec![]];
142142
//!
143-
//! assert_eq!(shortest_path(&graph, 0, 1), 1);
144-
//! assert_eq!(shortest_path(&graph, 0, 3), 3);
145-
//! assert_eq!(shortest_path(&graph, 3, 0), 7);
146-
//! assert_eq!(shortest_path(&graph, 0, 4), 5);
147-
//! assert_eq!(shortest_path(&graph, 4, 0), usize::MAX);
143+
//! assert_eq!(shortest_path(&graph, 0, 1), Some(1));
144+
//! assert_eq!(shortest_path(&graph, 0, 3), Some(3));
145+
//! assert_eq!(shortest_path(&graph, 3, 0), Some(7));
146+
//! assert_eq!(shortest_path(&graph, 0, 4), Some(5));
147+
//! assert_eq!(shortest_path(&graph, 4, 0), None);
148148
//! }
149149
//! ```
150150

0 commit comments

Comments
 (0)