Skip to main content

StringRange

Struct StringRange 

Source
pub struct StringRange {
    pub start_offset: u32,
    pub end_offset: u32,
}
Expand description

Identifies a substring within a given text using character offsets.

Uses zero-based Unicode character offsets where start_offset is inclusive and end_offset is exclusive.

Commonly used in autocomplete responses to indicate which portions of text matched the user’s input, allowing UI highlighting of relevant segments.

Note: Offsets are Unicode character positions, not byte positions, which is important for proper handling of multi-byte characters.

Fields§

§start_offset: u32

Zero-based offset of the first Unicode character (inclusive).

The starting position of the substring within the parent text.

§end_offset: u32

Zero-based offset of the last Unicode character (exclusive).

The ending position of the substring, where the character at this position is not included in the range.

Implementations§

Source§

impl StringRange

Source

pub fn start_offset(&self) -> &u32

Zero-based offset of the first Unicode character (inclusive).

The starting position of the substring within the parent text.

Source

pub fn end_offset(&self) -> &u32

Zero-based offset of the last Unicode character (exclusive).

The ending position of the substring, where the character at this position is not included in the range.

Source§

impl StringRange

Source

pub fn start_offset_mut(&mut self) -> &mut u32

Zero-based offset of the first Unicode character (inclusive).

The starting position of the substring within the parent text.

Source

pub fn end_offset_mut(&mut self) -> &mut u32

Zero-based offset of the last Unicode character (exclusive).

The ending position of the substring, where the character at this position is not included in the range.

Source§

impl StringRange

Source

pub fn set_start_offset(&mut self, val: u32) -> &mut Self

Zero-based offset of the first Unicode character (inclusive).

The starting position of the substring within the parent text.

Source

pub fn set_end_offset(&mut self, val: u32) -> &mut Self

Zero-based offset of the last Unicode character (exclusive).

The ending position of the substring, where the character at this position is not included in the range.

Source§

impl StringRange

Source

pub fn builder() -> StringRangeBuilder

Create an instance of StringRange using the builder syntax

Source§

impl StringRange

Source

pub const fn new(start_offset: u32, end_offset: u32) -> Self

Creates a new StringRange from start and end offsets.

Creates a range where start_offset is inclusive and end_offset is exclusive, matching standard range semantics.

Use this when constructing ranges from API responses or when manually specifying text segments to highlight.

Source

pub const fn start(self) -> u32

Returns the start offset (inclusive).

The zero-based Unicode character position where the range begins.

Source

pub const fn end(self) -> u32

Returns the end offset (exclusive).

The zero-based Unicode character position where the range ends. The character at this position is not included in the range.

Source

pub const fn len(self) -> u32

Returns the length of the range in characters.

Calculates the number of Unicode characters covered by this range.

Use this to determine how many characters to highlight or extract.

Source

pub const fn is_empty(self) -> bool

Checks if the range is empty (zero length).

Returns true if the start and end offsets are equal, indicating a zero-length range that covers no characters.

Source

pub const fn range(self) -> Range<usize>

Converts to a Rust standard Range<usize>.

Creates a Range<usize> for use with string slicing operations.

Use this when you need to extract or highlight the substring using Rust’s standard range syntax. Returns None if either offset is negative.

Trait Implementations§

Source§

impl Clone for StringRange

Source§

fn clone(&self) -> StringRange

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StringRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for StringRange

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for StringRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the range as [start..end].

Uses Rust’s standard range notation for machine-readable output. Suitable for debugging, logging, and serialization to text formats.

Source§

impl From<&Range<usize>> for StringRange

Source§

fn from(range: &Range<usize>) -> Self

Converts a borrowed &Range<usize> to a StringRange.

Creates a StringRange from a borrowed standard Rust range.

Use this when you want to convert a range reference without consuming the original.

Source§

impl From<Range<usize>> for StringRange

Source§

fn from(range: Range<usize>) -> Self

Converts a Rust Range<usize> to a StringRange.

Creates a StringRange from a standard Rust range.

Use this when constructing API requests from range-based string operations or when you have computed offsets using Rust’s range syntax.

Source§

impl PartialEq for StringRange

Source§

fn eq(&self, other: &StringRange) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for StringRange

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&StringRange> for Range<usize>

Source§

fn try_from(string_range: &StringRange) -> Result<Self, Self::Error>

Converts a borrowed &StringRange to a Rust Range<usize>.

Attempts to convert the StringRange offsets to usize for use with string slicing without consuming the original.

§Errors

Returns an error if either offset is negative or too large to fit in a usize.

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

impl TryFrom<StringRange> for Range<usize>

Source§

fn try_from(string_range: StringRange) -> Result<Self, Self::Error>

Converts a StringRange to a Rust Range<usize>.

Attempts to convert the StringRange offsets to usize for use with string slicing.

§Errors

Returns an error if either offset is negative or too large to fit in a usize.

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

impl Copy for StringRange

Source§

impl Eq for StringRange

Source§

impl StructuralPartialEq for StringRange

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,