Struct Slice

pub struct Slice {
    pub start: isize,
    pub end: Option<isize>,
    pub step: isize,
}
Expand description

A slice specification for a single tensor dimension.

This struct represents a range with an optional step, used for advanced indexing operations on tensors. It is typically created using the s! macro rather than constructed directly.

§Fields

  • start - The starting index (inclusive). Negative values count from the end.
  • end - The ending index (exclusive). None means to the end of the dimension.
  • step - The stride between elements. Must be non-zero.

§Index Interpretation

  • Positive indices: Count from the beginning (0-based)
  • Negative indices: Count from the end (-1 is the last element)
  • Bounds checking: Indices are clamped to valid ranges

§Step Behavior

  • Positive step: Traverse forward through the range
  • Negative step: Traverse backward through the range
  • Step size: Determines how many elements to skip

§Examples

While you typically use the s! macro, you can also construct slices directly:

use burn_tensor::Slice;

// Equivalent to s![2..8]
let slice1 = Slice::new(2, Some(8), 1);

// Equivalent to s![0..10;2]
let slice2 = Slice::new(0, Some(10), 2);

// Equivalent to s![..;-1] (reverse)
let slice3 = Slice::new(0, None, -1);

See also the s! macro for the preferred way to create slices.

Fields§

§start: isize

Slice start index.

§end: Option<isize>

Slice end index (exclusive).

§step: isize

Step between elements (default: 1).

Implementations§

§

impl Slice

pub const fn new(start: isize, end: Option<isize>, step: isize) -> Slice

Creates a new slice with start, end, and step

pub const fn full() -> Slice

Creates a slice that represents the full range.

pub fn index(idx: isize) -> Slice

Creates a slice that represents a single index

pub fn with_step(start: isize, end: Option<isize>, step: isize) -> Slice

Creates a slice with a custom step

pub fn from_range_stepped<R>(range: R, step: isize) -> Slice
where R: Into<Slice>,

Creates a slice from a range with a specified step

pub fn step(&self) -> isize

Returns the step of the slice

pub fn range(&self, size: usize) -> Range<usize>

Returns the range for this slice given a dimension size

pub fn to_range(&self, size: usize) -> Range<usize>

Convert this slice to a range for a dimension of the given size.

§Arguments
  • size - The size of the dimension to slice.
§Returns

A Range<usize> representing the slice bounds.

pub fn to_range_and_step(&self, size: usize) -> (Range<usize>, isize)

Converts the slice into a range and step tuple

pub fn is_reversed(&self) -> bool

Returns true if the step is negative

pub fn output_size(&self, dim_size: usize) -> usize

Calculates the output size for this slice operation

Trait Implementations§

§

impl Clone for Slice

§

fn clone(&self) -> Slice

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
§

impl Debug for Slice

§

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

Formats the value using the given formatter. Read more
§

impl Default for Slice

§

fn default() -> Slice

Returns the “default value” for a type. Read more
§

impl<'de> Deserialize<'de> for Slice

§

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

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

impl<I> From<Range<I>> for Slice
where I: AsIndex,

§

fn from(r: Range<I>) -> Slice

Converts to this type from the input type.
§

impl<I> From<RangeFrom<I>> for Slice
where I: AsIndex,

§

fn from(r: RangeFrom<I>) -> Slice

Converts to this type from the input type.
§

impl From<RangeFull> for Slice

§

fn from(_: RangeFull) -> Slice

Converts to this type from the input type.
§

impl<I> From<RangeInclusive<I>> for Slice
where I: AsIndex + Copy,

§

fn from(r: RangeInclusive<I>) -> Slice

Converts to this type from the input type.
§

impl<I> From<RangeTo<I>> for Slice
where I: AsIndex,

§

fn from(r: RangeTo<I>) -> Slice

Converts to this type from the input type.
§

impl<I> From<RangeToInclusive<I>> for Slice
where I: AsIndex,

§

fn from(r: RangeToInclusive<I>) -> Slice

Converts to this type from the input type.
§

impl From<i32> for Slice

§

fn from(i: i32) -> Slice

Converts to this type from the input type.
§

impl From<isize> for Slice

§

fn from(i: isize) -> Slice

Converts to this type from the input type.
§

impl From<usize> for Slice

§

fn from(i: usize) -> Slice

Converts to this type from the input type.
§

impl Hash for Slice

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl PartialEq for Slice

§

fn eq(&self, other: &Slice) -> 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.
§

impl Serialize for Slice

§

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

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

impl Copy for Slice

§

impl Eq for Slice

§

impl StructuralPartialEq for Slice

Auto Trait Implementations§

§

impl Freeze for Slice

§

impl RefUnwindSafe for Slice

§

impl Send for Slice

§

impl Sync for Slice

§

impl Unpin for Slice

§

impl UnwindSafe for Slice

Blanket Implementations§

§

impl<T> Adaptor<()> for T

§

fn adapt(&self)

Adapt the type to be passed to a metric.
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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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

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

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

§

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

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

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

§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> SliceArg<1> for T
where T: Into<Slice>,

§

fn into_slices(self, shape: Shape) -> [Slice; 1]

Convert to an array of slices with clamping to shape dimensions
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, 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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> CacheKey for T

§

impl<T> CacheValue for T

§

impl<T> CubeComptime for T
where T: Debug + Hash + Eq + Clone + Copy,

Source§

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

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,