Slicing
Slicing selects or partitions a portion of an indexed sequence or multidimensional array, often along specified indices or dimensions.
100 Go Mistakes and How to Avoid Them
Slicing is the Go operation that selects a half-open range of elements from an array or slice.
- How it is used
- Uses slicing to explain unexpected mutations, memory retention, and races caused by shared backing arrays.
- How deep it goes
- Traces length, capacity, shared storage, and append behavior through examples, then examines GC retention of pointer-bearing elements.
- Examples
- Shows an appended subslice overwriting an original element and a two-element prefix retaining data from 1,000 structs.
- What you can do after
- Diagnose/debug with it append side effects and retained memory, then choose capacity limits, copies, or clearing as appropriate.
Read how 100 Go Mistakes and How to Avoid Them explains slicing →
Learn Rust in a Month of Lunches
A slice is a dynamically sized borrowed view of a contiguous sequence of values.
- How it is used
- Uses borrowed views to work with portions of arrays, strings, and vectors.
- How deep it goes
- Explains range endpoints, variable-sized slices, ownership, and borrowed-string lifetimes through examples and compiler feedback.
- Examples
- Shows array-range outputs, a compiler correction from `str` to `&str`, and vector chunks and windows.
- What you can do after
- Practice/perform array slicing with ranges and choose chunks, windows, or vector splitting for borrowed pieces.
Read how Learn Rust in a Month of Lunches explains slicing →
Rust in Action: Systems programming concepts and techniques
A slice is a dynamically sized, fixed-length view of contiguous elements that does not own or resize the underlying storage.
- How it is used
- Uses slices as size-independent views of array data and as a way to work with byte and text data.
- How deep it goes
- Defines fixed-length, dynamically sized slices and explains referenced pointer-and-length representation, without a broad treatment of slicing operations.
- Examples
- Shows a byte-array range receiving opcodes through `copy_from_slice()` and discusses `[u8]` for potentially invalid text input.
- What you can do after
- Apply it via `copy_from_slice()` to copy bytes into a selected array range.
Read how Rust in Action: Systems programming concepts and techniques explains slicing →
Go in Action, Second Edition
A slice expression is Go syntax that creates a slice view over an index range of an array or slice while sharing the underlying storage.
- How it is used
- Selects part of an existing array or slice for processing without copying its elements.
- How deep it goes
- Explains two- and three-index expressions through backing storage, length, and capacity, with examples of each form.
- Examples
- Uses command-line filenames and a subset of sensor readings, alongside indexed slice and array examples.
- What you can do after
- Apply it via Go slice expressions to pass selected data to functions and constrain a view’s capacity.
Refactoring to Rust
A slice is a non-owning view of a contiguous region of elements represented by a pointer and a length.
- How it is used
- Uses slices to borrow substrings and C-owned NGINX request data without reallocating.
- How deep it goes
- Defines pointer-and-length views, explains their lifetimes, and shows the raw-pointer operations used to construct a request-body view.
- Examples
- Illustrates multiple `&str` views into one `String` and a string slice borrowed from NGINX request buffers.
- What you can do after
- Apply it via Rust `&str` and `std::slice::from_raw_parts` to view existing data while respecting its lifetime.
Code Like a Pro in Rust
A Rust slice is a dynamically sized view of a contiguous region of values of one type, represented with a pointer and a length.
- How it is used
- Uses borrowed views to work with arrays, vectors, and strings without requiring ownership of their storage.
- How deep it goes
- Works through array splitting, vector coercion, borrow-checker conflicts, and the standard-library implementation of fast slice copying.
- Examples
- Shows a 64-byte array split at index 32, a rejected vector resize, and a benchmark copying ten million integers.
- What you can do after
- Apply slices to borrow and split sequences, avoid conflicting vector mutations, and select `copy_from_slice()` for eligible bulk copies.
Tiny Python Projects: Learn coding and testing with puzzles and games
- How it is used
- Uses slices to extract, rearrange, and reconstruct portions of strings and lists during small Python programs.
- How deep it goes
- Explains zero-based indexes, omitted boundaries, excluded stops, negative indexes, and immutable-string workarounds, but not tensor-specific slicing.
- Examples
- Demonstrates prefixes, suffixes, list subranges, string replacement, verse gifts, word stemming, and scrambled word middles.
- What you can do after
- Apply it via Python slice notation to select subranges and reconstruct strings without direct character assignment.
Read how Tiny Python Projects: Learn coding and testing with puzzles and games explains slicing →
Learn Go with Pocket-Sized Projects
A slice is a dynamically sized view over the elements of an underlying Go array, represented by a pointer, length, and capacity.
- How it is used
- Uses slices to collect, iterate over, sort, and test variable-length collections in practical Go programs.
- How deep it goes
- Explains arrays, pointers, length, capacity, make, append, nil slices, ranging, and sorting operations.
- Examples
- Demonstrates shared-book collection, book recommendations, JSON input, slice initialization, ranging, sorting, and table-driven tests.
- What you can do after
- Build it from scratch by creating, growing, iterating over, sorting, and testing slices in Go applications.
Read how Learn Go with Pocket-Sized Projects explains slicing →
Python Workout: 50 ten-minute exercises
- How it is used
- Uses slicing to retrieve and process parts of Python strings, lists, tuples, and other sequence-like values.
- How deep it goes
- Explains bounds, steps, omitted indexes, type preservation, and forgiving ranges, but does not descend into implementation internals.
- Examples
- Uses alphabetic strings, first-and-last sequence functions, accumulation with items[1:], Pig Latin, and hexadecimal reversal.
- What you can do after
- Apply Python slice syntax to select ranges, steps, prefixes, suffixes, and remaining sequence elements in runnable code.
Read how Python Workout: 50 ten-minute exercises explains slicing →
Get Programming: Learn to code with Python
- How it is used
- Uses slicing to select string substrings and extract first and last names for a name-mashup program.
- How deep it goes
- Defines start, stop, and step, explains excluded stop bounds and step direction, and applies slicing to split names.
- Examples
- Uses student records, character groups in “snap crackle pop,” and first- and last-name extraction.
- What you can do after
- Apply slicing to extract substrings and separate first and last names.
Read how Get Programming: Learn to code with Python explains slicing →
Python How-To
- How it is used
- Retrieves subsequences from sequence data and supports modifying subsequences in mutable sequences.
- How deep it goes
- Explains bounds, omitted indices, strides, slice objects, and mutable-sequence edits with practical examples.
- Examples
- Uses fruit and number lists, monthly revenue, and a challenge involving strings, tuples, and ranges.
- What you can do after
- Apply Python slicing to select subsequences with bounds and steps, reuse slice objects, and edit mutable sequences.
The Quick Python Book, Fourth Edition
- How it is used
- Uses Python sequence notation to extract portions of lists and strings, and to edit list regions.
- How deep it goes
- Explains slice bounds, omitted endpoints, negative indices, empty reverse ranges, and list assignment; treats lists and strings rather than multidimensional arrays.
- Examples
- Demonstrates extracting list subsegments, copying and resizing lists, and chopping a newline from a string.
- What you can do after
- Apply slicing to extract, copy, replace, insert, or remove list regions and extract string substrings.
Read how The Quick Python Book, Fourth Edition explains slicing →
Learn AI-Assisted Python Programming, Second Edition
- How it is used
- Selects portions of strings and lists, then supports word-generation subtasks.
- How deep it goes
- Defines exclusive endpoints and omitted bounds, then applies slicing within insertion and replacement functions.
- Examples
- Demonstrates books[1:3], omitted bounds, and word insertion or replacement using sliced strings.
- What you can do after
- Apply slicing in Python to extract list or string ranges and construct modified words.
Read how Learn AI-Assisted Python Programming, Second Edition explains slicing →
Idiomatic Rust
A slice is a dynamically sized view of a contiguous sequence without owning the underlying storage.
- How it is used
- Uses slices to expose contiguous data through accessors and accept varied collection types in function inputs.
- How deep it goes
- Compares Vec- and slice-based input signatures and demonstrates a slice-returning accessor, staying at the interface level.
- Examples
- Demonstrates a pizza-toppings accessor, flexible collection inputs, and string reversal using &str.
- What you can do after
- Apply slices by accepting &[T] instead of &Vec<T> in library function interfaces.
Julia as a Second Language
Array slicing is the selection of one or more elements or subarrays from an array by specifying index ranges or positions.
- How it is used
- Uses index positions and ranges to extract or modify elements, rows, columns, and subarrays.
- How deep it goes
- Explains one- and two-dimensional syntax, begin/end indexing, copy semantics, views, and underlying indexing functions.
- Examples
- Demonstrates vector ranges, begin/end positions, matrix row and column selection, copied slices, and @view mutations.
- What you can do after
- Apply it via Julia indexing syntax to select array data, preserve or share storage, and avoid unnecessary copies.
Go in Practice, Second Edition
- How it is used
- Uses slices for homogeneous, variable-length collections in command-line validation and generic data processing.
- How deep it goes
- Defines slices against arrays and demonstrates validation and filtering, without detailing range expressions or slice internals.
- Examples
- Demonstrates validating language codes and filtering slices of strings and integers.
- What you can do after
- Apply slices to validate allowed values and filter collections with generic functions.
Related concepts
- arraycontrasts with · An array is an indexed, ordered collection of elements arranged in one or more dimensions, with elements accessed by position.
- built-in functionused by · A built-in function is a predefined callable operation supplied by a programming language, runtime, library, or execution environment.
- slice lengthmeasured by · Slice length is the number of elements currently contained in a slice and accessible through its indices.
- os.ReadFileproduced by · os.ReadFile is a Go standard-library function that reads an entire named file and returns its contents as a byte slice and an error.
- stringuses · A string is a programming-language value representing an ordered sequence of characters or character units as text.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.















