Merge pull request #60 from sigp/shuffle_underflow

Fix underflow in shuffle with empty list
This commit is contained in:
Age Manning 2018-10-26 09:53:09 +02:00 committed by GitHub
commit 9bd092e419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,7 +34,7 @@ pub fn shuffle<T>(
return Ok(list); return Ok(list);
} }
for i in 0..(list.len() - 1) { for i in 0..(list.len().saturating_sub(1)) {
let n = list.len() - i; let n = list.len() - i;
let j = rng.rand_range(n as u32) as usize + i; let j = rng.rand_range(n as u32) as usize + i;
list.swap(i, j); list.swap(i, j);