Skip to content

Provide method find_slice() for &[T] #15853

Closed
@nwin

Description

@nwin

&str has find_str(). It would be handy to have such a method for slices as well. This is for example useful for work with non-utf8 based strings.

A simple implementation for std::slice::ImmutableEqVector could be:

/// Returns the index of the first matching subslice
fn find_slice<T: PartialEq>(&self, needle: &[T]) -> Option<uint> {
    for i in range(0, this.len()) {
        if this.slice_from(i).starts_with(needle) {
            return Some(i)
        }
    }
    None
}

If desired I could file a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions