Skip to content

proposal: slices: add Shift #64103

@pgaskin

Description

@pgaskin

I propose adding the following function to slices:

// Shift removes the first element from s, returning false if the slice is empty.
func Shift[S ~[]E, E any](s S) (first E, rest S, ok bool) {
	if len(s) == 0 {
		rest = s
	} else {
		first, rest, ok = s[0], s[1:], true
	}
	return
}

Like #64095, this is useful for parsing stuff.


Update: I think this is better without the ok return value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions