.\" Made by website/plain/build.mjs from website/words/slice.md; change that file. .TH SG-SLICE 7 2026-11-28 "SG" "SG reference" .SH NAME T[] \- a slice: a window onto items that something else owns .SH SYNOPSIS .TP \fBread T[]\fR a slice to look at: a pointer and a length .TP \fBref T[]\fR a slice whose items may be written, sorted or turned round .TP \fBread xs, ref xs\fR a whole list or array, lent as a slice .TP \fBxs[a..b], xs[..b], xs[a..], xs[..]\fR a part, to look at; the item at b is not in it .TP \fBxs[^2..], xs[1..^1]\fR ends counted from the end .TP \fBref xs[a..b]\fR a part, to write .TP \fBtext.asBytes()\fR the bytes of a text, as a read byte[] .TP \fBs[i], s.length(), s.get(i), for x in s\fR as on a list .TP \fBread T[]?\fR a slice, or null .SH DESCRIPTION A slice is a window onto items that something else owns: a list, an array, the bytes of a text, or another slice. It is a pointer and a length, and taking one copies nothing. A \fBread T[]\fR only looks at the items. A \fBref T[]\fR may also write them, sort them or turn them round, but not add or take away any: how many there are is up to the owner. .PP \fBT[]\fR alone is not a type, since a slice never owns anything. A list that owns its items is a \fBList\fR, and a fixed number of them is an array, \fBT[4]\fR\&. A function that only needs the items takes \fBread T[]\fR or \fBref T[]\fR, and then takes a list, an array and any part of either. The call says what it lends: \fBread xs\fR the whole, \fBxs[1..3]\fR a part (a range is already a borrow), and \fBref xs[1..3]\fR a part to write. .PP A slice is a borrow, and the compiler checks it as one. While a slice is still used, the list it looks at cannot grow, be cleared or end, since its items could move or be freed. Two \fBref\fR slices of one list may be held at once when they share no item, as \fBref xs[..k]\fR beside \fBref xs[k..]\fR\&. .PP An index into a slice is checked against the slice's own length, and the ends of a range against the length of what it is taken from, in every build: a wrong one stops the program with a panic, never reads memory outside. \fBget(i)\fR gives \fBnull\fR instead. A slice has the methods of a list that only look (\fBlength\fR, \fBget\fR, \fBindexOf\fR, \fBcontains\fR, \fBbinarySearch\fR), each counting from the start of the slice; through a \fBref T[]\fR it also has \fBsort\fR, \fBreverse\fR and \fBcopyFrom\fR\&. .SH EXAMPLES .SS "Lend a list or an array" .RS 4 .nf int total(read int[] values) { var sum = 0 for v in values { sum += v } return sum } void main() { var list = List { 1, 2, 3, 4, 5 } var array = int[4] { 10, 20, 30, 40 } print(total(read list)) print(total(read array)) print(total(list[1..3])) print(total(array[^2..])) } .fi .RE .PP .RS 4 .nf 15 100 5 70 .fi .RE .PP One function takes the list, the array, and a part of each. \fBlist[1..3]\fR is the items at 1 and 2, and \fBarray[^2..]\fR the last two. .SS "A part of a list" .RS 4 .nf void main() { var days = List { "mon", "tue", "wed", "thu", "fri" } print(days[1..3]) print(days[..2]) print(days[3..]) print(days[^2..]) print(days[1..^1]) var middle = days[1..4] print($"{middle.length()} {middle[0]} {middle[^1]}") print(middle.get(5) ?? "none") print(middle.indexOf("thu") ?? -1) print(middle[1..]) } .fi .RE .PP .RS 4 .nf ["tue", "wed"] ["mon", "tue"] ["thu", "fri"] ["thu", "fri"] ["tue", "wed", "thu"] 3 tue thu none 2 ["wed", "thu"] .fi .RE .PP \fBmiddle\fR is a \fBread string[]\fR that looks at three items of \fBdays\fR\&. Its indexes start at its own first item: \fBmiddle[0]\fR is \fB"tue"\fR, and \fBindexOf\fR finds \fB"thu"\fR at 2. A range of a slice is a slice again. .SS "Write through a slice" .RS 4 .nf void fill(ref int[] target, int value) { for i in 0..target.length() { target[i] = value } } void main() { var xs = List { 9, 4, 7, 1, 8, 2 } xs[..3].sort() print(xs) fill(ref xs[4..], 0) print(xs) var tail = ref xs[3..] tail.reverse() tail[0] = 5 print(xs) } .fi .RE .PP .RS 4 .nf [4, 7, 9, 1, 8, 2] [4, 7, 9, 1, 0, 0] [4, 7, 9, 5, 0, 1] .fi .RE .PP \fBxs[..3].sort()\fR puts only the first three items in order, where they are. \fBref xs[4..]\fR lends the last two to be written, and \fBtail\fR keeps a \fBref int[]\fR in a variable; every change lands in \fBxs\fR\&. .SS "The bytes of a text" .RS 4 .nf nint countByte(read byte[] data, byte wanted) { nint count = 0 for b in data { if b == wanted { count += 1 } } return count } void main() { var line = "a,b,,c" var bytes = line.asBytes() print(bytes.length()) print(bytes[1]) print(countByte(bytes, ',')) print(countByte(bytes[2..], ',')) } .fi .RE .PP .RS 4 .nf 6 44 3 2 .fi .RE .PP \fBasBytes()\fR gives the bytes of the text as a \fBread byte[]\fR, with no copy. What \fBread string\fR is for text, \fBread byte[]\fR is for raw data. .SS "Items that are structs" .RS 4 .nf struct Order { string customer long amount } long sum(read Order[] orders) { long total = 0 for o in orders { total += o.amount } return total } void discount(ref Order[] orders, long percent) { for i in 0..orders.length() { orders[i].amount -= orders[i].amount * percent / 100 } } void main() { var orders = List { Order { customer = "ada", amount = 1200 }, Order { customer = "grace", amount = 300 }, Order { customer = "linus", amount = 80 } } print(sum(read orders)) discount(ref orders[1..], 50) print(sum(read orders)) print(orders[^1].amount) } .fi .RE .PP .RS 4 .nf 1580 1390 40 .fi .RE .PP A \fBfor\fR over a slice lends each item, so no order is copied. Through a \fBref Order[]\fR the fields of the items can be written, and only the orders in the slice change. .SS "Two parts of one list at once" .RS 4 .nf void main() { var xs = List { 1, 2, 3, 10, 20, 30 } var half = xs.length() / 2 var left = ref xs[..half] var right = ref xs[half..] for i in 0..left.length() { left[i] += right[i] right[i] = 0 } print(xs) } .fi .RE .PP .RS 4 .nf [11, 22, 33, 0, 0, 0] .fi .RE .PP \fBleft\fR and \fBright\fR are both written, and both borrow \fBxs\fR\&. That is allowed because they share no item; it is checked when \fBright\fR is taken, and a panic if they do. Rust writes this with \fBsplit_at_mut\fR\&. .SS "A slice that may be missing" .RS 4 .nf read int[]? firstRun(read int[] items, int value) { var start = items.indexOf(value) ?? return null var end = start while end < items.length() && items[end] == value { end += 1 } return items[start..end] } void main() { var xs = List { 4, 7, 7, 7, 2 } var run = firstRun(read xs, 7) if run is not null { print($"{run.length()} at {run}") } print(firstRun(read xs, 9) is null) } .fi .RE .PP .RS 4 .nf 3 at [7, 7, 7] true .fi .RE .PP \fBread int[]?\fR is a slice or \fBnull\fR\&. The slice that comes back looks at \fBxs\fR, as the one given did. After \fBif run is not null\fR, \fBrun\fR is a \fBread int[]\fR\&. Only a \fBread\fR slice may be missing: a \fBref int[]?\fR is refused. .SH "WHAT STOPS THE PROGRAM" .SS "An index past the end of the slice" .RS 4 .nf void main() { var xs = List { 5, 3, 8, 1 } var part = xs[1..3] print(part[2]) } .fi .RE .PP .RS 4 .nf panic: index 2 is out of range: the length is 2 .fi .RE .PP The list has an item at 2, but \fBpart\fR has only two items. An index is checked against the slice, not against what it looks at. .SS "A range past the end" .RS 4 .nf void main() { var xs = List { 5, 3, 8, 1 } var n = xs.length() + 2 print(xs[1..n]) } .fi .RE .PP .RS 4 .nf panic: range 1..6 is out of range: the length is 4 .fi .RE .SS "Two ref parts that share items" .RS 4 .nf void addInto(ref int[] into, read int[] from) { for i in 0..from.length() { into[i] += from[i] } } void main() { var xs = List { 1, 2, 3, 10, 20, 30 } var cut = xs.length() / 2 + 1 addInto(ref xs[..cut], xs[cut - 2..]) print(xs) } .fi .RE .PP .RS 4 .nf panic: `xs[cut - 2..]` shares items 2..4 with `xs[..cut]`, lent to this call too .fi .RE .PP Where the ends depend on the data, the check is made as the program runs. .SH "WHAT THE COMPILER REFUSES" .SS "T[] alone" .RS 4 .nf int total(int[] values) { var sum = 0 for v in values { sum += v } return sum } void main() { var xs = List { 1, 2, 3 } print(total(read xs)) } .fi .RE .PP .RS 4 .nf error: `int[]` is not a type on its own: a slice only looks at items that something else owns --> example.sg:1:11 1 | int total(int[] values) { | ^^^^^ hint: `List` to own the items, `read int[]` for a window onto them .fi .RE .PP In C# \fBint[]\fR is an array that owns its items. In SG it would be a window with no owner, so \fBread\fR or \fBref\fR is written. .SS "A list given without read" .RS 4 .nf int total(read int[] values) { var sum = 0 for v in values { sum += v } return sum } void main() { var xs = List { 1, 2, 3 } print(total(xs)) } .fi .RE .PP .RS 4 .nf error: expected `read int[]`, found `List` --> example.sg:11:17 11 | print(total(xs)) | ^^ hint: lend it with `read`: `read xs` .fi .RE .SS "Changing a list a slice still looks at" .RS 4 .nf void main() { var xs = List { 5, 3, 8, 1 } var part = xs[1..3] xs.add(9) print(part) } .fi .RE .PP .RS 4 .nf error: `xs` cannot be changed here: `part` borrows from it and is used again later --> example.sg:4:5 4 | xs.add(9) | ^^ `part` borrowed it here: --> example.sg:3:16 3 | var part = xs[1..3] | ^^^^^^^^ hint: finish with `part` before this line: a borrow ends at its last use .fi .RE .PP \fBadd\fR may move the items to a larger block, and \fBpart\fR would point at the old one. .SS "A slice of a list that ends first" .RS 4 .nf read int[] firstTwo() { var xs = List { 5, 3, 8, 1 } return xs[..2] } void main() { print(firstTwo()) } .fi .RE .PP .RS 4 .nf error: this returns a borrow of `xs`, and `xs` is gone when the function returns --> example.sg:3:12 3 | return xs[..2] | ^^^^^^^ hint: return the value itself instead of a borrow of it .fi .RE .PP \fBxs\fR is freed when \fBfirstTwo\fR returns. A function gives back a slice only of what it was lent. .SS "Sorting through a read slice" .RS 4 .nf void tidy(read int[] values) { values.sort() } void main() { var xs = List { 5, 3, 8, 1 } tidy(read xs) print(xs) } .fi .RE .PP .RS 4 .nf error: `sort` changes the slice, but it is reached through a `read` borrow --> example.sg:2:5 2 | values.sort() | ^^^^^^ hint: lend it as a `ref int[]`: `ref xs` or `ref xs[a..b]` .fi .RE .SS "Two ref parts that always overlap" .RS 4 .nf void main() { var xs = List { 1, 2, 3, 4, 5 } var left = ref xs[0..3] var right = ref xs[2..5] left[0] = right[0] print(xs) } .fi .RE .PP .RS 4 .nf error: `xs[2..5]` shares items with `xs[0..3]` every time this runs, and a `ref` borrow must be the only one of its items --> example.sg:4:17 4 | var right = ref xs[2..5] | ^^^^^^^^^^^^ `left` borrowed it here, and is used again later: --> example.sg:3:16 3 | var left = ref xs[0..3] | ^^^^^^^^^^^^ hint: lend items that do not overlap, as `ref xs[..k]` beside `ref xs[k..]` do .fi .RE .PP When the ends are numbers, the overlap is found when the program is compiled. .SS "read in front of a range" .RS 4 .nf void main() { var xs = List { 1, 2, 3, 4, 5 } print(read xs[1..3]) } .fi .RE .PP .RS 4 .nf error: a range is already a borrow, so `read` is not written in front of it --> example.sg:3:11 3 | print(read xs[1..3]) | ^^^^^^^^^^^^^ hint: `xs[1..3]` .fi .RE .SH "SEE ALSO" .BR sg-read (7), .BR sg-ref (7), .BR sg-list (7), .BR sg-range (7), .BR sg-index-from-end (7), .BR sg-string (7), .BR sg-nullable (7) .PP https://sg\-lang.org/reference/slice.html