pub unsafe fn concat<'a>(a: &'a str, b: &'a str) -> Result<&'a str, Error>
Expand description
Concatenate two string slices if they are adjacent.
If two strs are adjacent to each other in memory, this function concatenates both, creating a single str.
§Errors
Returns Err
if the two slices aren’t adjacent, a
is after b
, or if
a
is too long for proper concatenation (longer than isize::MAX
).
§Safety
The provided slices must come from the same underlying allocation. The adjacency test can not reliably differentiate between the one-past-the-end pointer of one allocation and the start of another. However, all slices must be within a single allocation.
§Examples
Correct usage:
let s = "0123456789";
unsafe {
// SAFETY: slices from the same str originally.
assert_eq!("0123456", concat(&s[..5], &s[5..7]).unwrap());
}
Non-adjacent string slices:
let s = "0123456789";
unsafe {
// SAFETY: slices from the same str originally.
assert_eq!(Err(Error::NotAdjacent), concat(&s[..5], &s[6..7]))
}