#[unsafe(no_mangle)]pub const unsafe extern "C" fn nstd_core_str_substr(
str: &NSTDStr,
range: NSTDURange,
) -> NSTDOptionalStrAvailable on crate feature
core only.Expand description
Creates a substring of an existing string slice.
§Parameters:
-
const NSTDStr *str- The string slice to create the new substring from. -
NSTDURange range- The bounds of the new substring (indexed by bytes).
§Returns
NSTDOptionalStr substr - The new substring on success, or a “none” variant if the
result is not valid UTF-8.
§Panics
This operation can panic under the following circumstances:
-
range.startis greater thanrange.end. -
range.endis greater thanstr.len.
§Safety
str’s data must be valid for reads of at least str.len consecutive bytes.
§Example
use nstd_sys::core::{
range::NSTDURange,
str::{nstd_core_str_byte_len, nstd_core_str_from_raw_cstr, nstd_core_str_substr},
};
let s_str = "33marrow\0";
unsafe {
let str = nstd_core_str_from_raw_cstr(s_str.as_ptr().cast()).unwrap();
let range = NSTDURange {
start: 2,
end: nstd_core_str_byte_len(&str),
};
let marrow = nstd_core_str_substr(&str, range).unwrap();
assert!(nstd_core_str_byte_len(&marrow) == 6);
}