Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add Core Library to introduction section of the Sway book #6193

Merged
merged 20 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@ namespacing
unsafety
prioritizations
polymorphism
ContractId
ContractId
booleans
underflows
Codec
bool
str
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [The Fuel Toolchain](./introduction/fuel_toolchain.md)
- [A Forc Project](./introduction/forc_project.md)
- [Standard Library](./introduction/standard_library.md)
- [Core Library](./introduction/core_library.md)
- [Sway Language Standards](./introduction/sway_standards.md)
- [Examples](./examples/index.md)
- [Counter](./examples/counter.md)
Expand Down
44 changes: 44 additions & 0 deletions docs/book/src/introduction/core_library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Core Library

The Sway Core Library, like the name suggests contains core operators and logic for the primitive types of the Sway programming language. These traits and methods are an extension of the [primitive types](https://docs.fuel.network/docs/sway/basics/built_in_types/#primitive-types) `u8`, `u16`, `u32`, `u64`, `u256`, `str[]`, `str`, `bool` and , `b256` and can be used where appropriate.

The latest core library documentation can be found [here](https://fuellabs.github.io/sway/master/core/). If the latest version is not compatible please refer to the appropriate tagged release.

## Using the Core Library

Core library functionalities do not need to be explicitly imported and will work out of the box after creating any new Sway project with [`forc new`](../forc/commands/forc_new.md). The `use` keyword is simply not required.

Consider this example of using the modulo function for two like value types:

```sway
let val_1 = 10;
let val_2 = 2;
let result = val_1 % val_2;
```

## Core Library Prelude

The prelude contains a list of operations essential to all Sway programs. The latest version of the prelude can be found [here](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/prelude.sw).

- [`core::primitives::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/primitives.sw) a module for getting `max`, `min`, `bits` and `zero` for integers.
calldelegation marked this conversation as resolved.
Show resolved Hide resolved
- [`core::primitive_conversions::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/primitive_conversions.sw) a module for converting between unsigned integers sizes.
- [`core::raw_ptr::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/raw_ptr.sw) a module for dealing with pointers.
- [`core::raw_slice::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/raw_slice.sw) a module for converting types to raw slice
- [`core::ops::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/ops.sw) a module for operations like `add` or `subtract` and comparisons `equal` and `order`.
- [`core::storage::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/storage.sw) a module dealing with storage.
- [`core::str::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/str.sw) a module dealing with `str` slices like `len` or conversions like `from_str_array`.
- [`core::codec::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/codec.sw) a module to encode and decode data structures.

For the full list of traits and methods available for each primitive type, please refer to the chart below or the core library documentation [here](https://fuellabs.github.io/sway/master/core/index.html).

| Primitive Type | Description |
calldelegation marked this conversation as resolved.
Show resolved Hide resolved
| ------------------------------------------------------------------------------ | -------------------------------- |
| [b256](https://fuellabs.github.io/sway/master/core/primitive.b256.html) | 256 bits (32 bytes), i.e. a hash |
| [bool](https://fuellabs.github.io/sway/master/core/primitive.bool.html) | Boolean true or false |
| [str](https://fuellabs.github.io/sway/master/core/primitive.str.html) | String Slice |
| [str[0-63]](https://fuellabs.github.io/sway/master/core/primitive.str[0].html) | Fixed-length string |
| [u265](https://fuellabs.github.io/sway/master/core/primitive.u256.html) | 256-bit unsigned integer |
| [u64](https://fuellabs.github.io/sway/master/core/primitive.u64.html) | 64-bit unsigned integer |
| [u32](https://fuellabs.github.io/sway/master/core/primitive.u32.html) | 32-bit unsigned integer |
| [u16](https://fuellabs.github.io/sway/master/core/primitive.u16.html) | 16-bit unsigned integer |
| [u8](https://fuellabs.github.io/sway/master/core/primitive.u8.html) | 8-bit unsigned integer |
1 change: 1 addition & 0 deletions docs/book/src/introduction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ To get started with Forc and Sway smart contract development, install the Fuel t
- [The Fuel Toolchain](./fuel_toolchain.md)
- [A Forc Project](./forc_project.md)
- [Standard Library](./standard_library.md)
- [Core Library](./core_library.md)
- [Sway Language Standards](./sway_standards.md)
Loading