Use `saturating_sub` instead of `checked_sub.unwrap_or`

This commit is contained in:
Hiroki Noda 2018-06-04 01:42:09 +09:00
parent 2a55ff62f3
commit 289028850f
2 changed files with 2 additions and 2 deletions

View File

@ -122,7 +122,7 @@ fn parse_include_path(path: &str) -> LinkType<'static> {
let start = parts let start = parts
.next() .next()
.and_then(|s| s.parse::<usize>().ok()) .and_then(|s| s.parse::<usize>().ok())
.map(|val| val.checked_sub(1).unwrap_or(0)); .map(|val| val.saturating_sub(1));
let end = parts.next(); let end = parts.next();
let has_end = end.is_some(); let has_end = end.is_some();
let end = end.and_then(|s| s.parse::<usize>().ok()); let end = end.and_then(|s| s.parse::<usize>().ok());

View File

@ -50,7 +50,7 @@ pub fn take_lines<R: RangeArgument<usize>>(s: &str, range: R) -> String {
let start = *range.start().unwrap_or(&0); let start = *range.start().unwrap_or(&0);
let mut lines = s.lines().skip(start); let mut lines = s.lines().skip(start);
match range.end() { match range.end() {
Some(&end) => lines.take(end.checked_sub(start).unwrap_or(0)).join("\n"), Some(&end) => lines.take(end.saturating_sub(start)).join("\n"),
None => lines.join("\n"), None => lines.join("\n"),
} }
} }