Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Indexing and Slicing #3

Open
sunjay opened this issue Jan 18, 2017 · 0 comments
Open

Indexing and Slicing #3

sunjay opened this issue Jan 18, 2017 · 0 comments

Comments

@sunjay
Copy link
Owner

sunjay commented Jan 18, 2017

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant