Skip to content

Commit 9892279

Browse files
committed
Auto merge of #75121 - tmiasko:str-slicing, r=Mark-Simulacrum
Avoid `unwrap_or_else` in str indexing This provides a small reduction of generated LLVM IR, and leads to a simpler assembly code. Closes #68874.
2 parents 63e3442 + 427634b commit 9892279

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

library/core/src/str/mod.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,10 @@ mod traits {
19231923
#[inline]
19241924
fn index(self, slice: &str) -> &Self::Output {
19251925
let (start, end) = (self.start, self.end);
1926-
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
1926+
match self.get(slice) {
1927+
Some(s) => s,
1928+
None => super::slice_error_fail(slice, start, end),
1929+
}
19271930
}
19281931
#[inline]
19291932
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
@@ -1995,7 +1998,10 @@ mod traits {
19951998
#[inline]
19961999
fn index(self, slice: &str) -> &Self::Output {
19972000
let end = self.end;
1998-
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
2001+
match self.get(slice) {
2002+
Some(s) => s,
2003+
None => super::slice_error_fail(slice, 0, end),
2004+
}
19992005
}
20002006
#[inline]
20012007
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
@@ -2068,7 +2074,10 @@ mod traits {
20682074
#[inline]
20692075
fn index(self, slice: &str) -> &Self::Output {
20702076
let (start, end) = (self.start, slice.len());
2071-
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
2077+
match self.get(slice) {
2078+
Some(s) => s,
2079+
None => super::slice_error_fail(slice, start, end),
2080+
}
20722081
}
20732082
#[inline]
20742083
fn index_mut(self, slice: &mut str) -> &mut Self::Output {

0 commit comments

Comments
 (0)