brain icon indicating copy to clipboard operation
brain copied to clipboard

Indexing and Slicing

Open sunjay opened this issue 9 years ago • 0 comments

Depends on #46 before implementation.

The assignment syntax on its own is quite limiting because you can only assign to buffers that are the same size as each other. Indexing allows us to slice and copy parts of buffers into parts of other buffers. This is convenient while remaining memory-safe and easy to reason about.

Support the following syntax:

let a: [u8; _] = "Hello, world";
let b: [u8; _] = "abc";

// outputs "ello"
stdout.write(a[1..5]);

// still invalid:
// a = b;
// b = a;

// After this, a will be "Habco, world"
a[1..4] = b; // equivalent a[1..4] = b[..]

// After this, b will be "abd"
b[-1] = a[-1];

// Error to assign different sizes
//a[1..5] = b[1..];

The range syntax [start..end] contains the range of cells from start to end-1. You can leave off the start or end of the range to imply either 0 or (size - 1) respectively. Using a negative index (idx) is the same as saying length - idx.

sunjay avatar Jan 18 '17 22:01 sunjay