#Get start and end of range without iterating.

1 messages · Page 1 of 1 (latest)

spark crown
#

Is it possible to get the start and end values of a RangeInclusive without iterating through it?

For context, I want to loop through a string and store the spans where a specific set of values appears. When displaying them later on, I intend to clone the string and highlight each value with a certain background colour. I have the following struct (not fully accurate, but it's enough):

pub struct HighlightSet<'source> {
    source: &'source str,
    highlights: Vec<RangeInclusive<usize>>,
}

If possible, I would like to avoid storing the start and end values in a custom-made struct or a tuple, as I feel like a range type makes what is being stored here clearer when looking at the code.

lusty pier
#

.start() and .end() methods I think

spark crown
#

Huh. My eyes slid right over those when I looked in the docs

lusty pier
#

happens haha

#

It's so weird to me that those are not public fields

#

Whereas the regular range has that

spark crown
#

The source code reveals the reason behind that decision:

#
pub struct RangeInclusive<Idx> {
    // Note that the fields here are not public to allow changing the
    // representation in the future; in particular, while we could plausibly
    // expose start/end, modifying them without changing (future/current)
    // private fields may lead to incorrect behavior, so we don't want to
    // support that mode.
    pub(crate) start: Idx,
    pub(crate) end: Idx,
    
    // ...
}
lusty pier
#

Hmm I see

#

But then what's different about the regular range ferrisThink

spark crown
#

The regular range is exclusive, so they can just use start >= end to check if it's exhausted or not

lusty pier
#

OH

spark crown
#

Anyway, thank you

lusty pier
spark crown
#

Dumb followup question: what happens if I #[derive(Clone)] on a struct that contains a reference?