Skip to content

Commit

Permalink
improve documentation of slices
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Jul 31, 2024
1 parent 715cb37 commit 514ddf8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
14 changes: 14 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'arrays'
source = 'root'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-54F92B42A645EA2B'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.24.5#e695606d8884a18664f6231681333a784e623bc9'
dependencies = ['core']
8 changes: 8 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "slices"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
7 changes: 7 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library;

// ANCHOR: syntax
fn syntax(s: &[u64]) -> u64 {
s.len()
}
// ANCHOR_END: syntax
14 changes: 14 additions & 0 deletions docs/reference/src/documentation/language/built-ins/slices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Slices

A slice is similar to an [array](arrays.md), in the sense that it is a contiguous sequence of elements of the same type.

Unlike arrays, slices cannot be allocated, because its size is unknown at compilation time. The only way to use slices is through references to a slice.

References to slice are "fat pointers" containing two items:

- a pointer to the first element of the slice;
- a `u64` with how many elements the slice has.

```sway
{{#include ../../../code/language/built-ins/slices/src/lib.sw:syntax}}
```

0 comments on commit 514ddf8

Please sign in to comment.