Skip to main content

Slice

Struct Slice 

Source
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§

Source§

impl Slice

Source

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

Creates a new slice with start, end, and step

Source

pub const fn full() -> Slice

Creates a slice that represents the full range.

Source

pub fn index(idx: isize) -> Slice

Creates a slice that represents a single index

Source

pub fn into_vec(self) -> Vec<isize>

Converts the slice to a vector.

Source

pub fn bound_to(self, size: usize) -> Slice

Clips the slice to a maximum size.

§Example
ⓘ
assert_eq!(
    Slice::new(0, None, 1).bound_to(10),
    Slice::new(0, Some(10), 1));
assert_eq!(
    Slice::new(0, Some(5), 1).bound_to(10),
    Slice::new(0, Some(5), 1));
assert_eq!(
    Slice::new(0, None, -1).bound_to(10),
    Slice::new(0, Some(-11), -1));
assert_eq!(
    Slice::new(0, Some(-5), -1).bound_to(10),
    Slice::new(0, Some(-5), -1));
Source

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

Creates a slice with a custom step

Source

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

Source

pub fn step(&self) -> isize

Returns the step of the slice

Source

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

Returns the range for this slice given a dimension size

Source

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.

Source

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

Converts the slice into a range and step tuple

Source

pub fn is_reversed(&self) -> bool

Returns true if the step is negative

Source

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

Calculates the output size for this slice operation

Trait Implementations§

Source§

impl Clone for Slice

Source§

fn clone(&self) -> Slice

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Slice

Source§

impl Debug for Slice

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Slice

Source§

fn default() -> Slice

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

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

Source§

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
Source§

impl Display for Slice

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Slice

Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<RangeFull> for Slice

Source§

fn from(_: RangeFull) -> Slice

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<i32> for Slice

Source§

fn from(i: i32) -> Slice

Converts to this type from the input type.
Source§

impl From<i64> for Slice

Source§

fn from(i: i64) -> Slice

Converts to this type from the input type.
Source§

impl From<isize> for Slice

Source§

fn from(i: isize) -> Slice

Converts to this type from the input type.
Source§

impl From<usize> for Slice

Source§

fn from(i: usize) -> Slice

Converts to this type from the input type.
Source§

impl FromStr for Slice

Source§

type Err = ExpressionError

The associated error which can be returned from parsing.
Source§

fn from_str(source: &str) -> Result<Slice, <Slice as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Slice

Source§

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
Source§

impl IntoIterator for Slice

Note: Unbounded Slices produce infinite iterators.

Source§

type Item = isize

The type of the elements being iterated over.
Source§

type IntoIter = SliceIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <Slice as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Slice

Source§

fn eq(&self, other: &Slice) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Slice

Source§

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
Source§

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 UnsafeUnpin for Slice

§

impl UnwindSafe for Slice

Blanket Implementations§

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> Boilerplate for T
where T: Copy + Send + Sync + Debug + PartialEq + 'static,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneExpand for T
where T: Clone,

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
Source§

impl<T> CompilationArg for T
where T: Clone + PartialEq + Eq + Hash + Debug + Send + Sync + 'static,

Source§

fn dynamic_cast<Arg>(&self) -> Arg
where Arg: CompilationArg,

Compilation args should be the same even with different element types. However, it isn’t possible to enforce it with the type system. So, we make the compilation args serializable and dynamically cast them. Read more
Source§

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

Source§

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

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

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

Source§

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

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

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

Source§

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

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoComptime for T

Source§

fn comptime(self) -> Self

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

fn into_slices(self, shape: &Shape) -> Vec<Slice>

Convert to an vec of slices with clamping to shape dimensions. Read more
Source§

impl<T> StoreKey for T

Source§

impl<T> StoreValue for T

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> TuneInputs for T
where T: Clone + Send + Sync + 'static,

Source§

type At<'a> = T

The concrete input type at lifetime 'a.
Source§

impl<T> Upcast<T> for T

Source§

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

Source§

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

Source§

fn vzip(self) -> V

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more