Skip to content

Commit

Permalink
move code samples into a file for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed May 13, 2024
1 parent 04f7cfa commit 39c89be
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
32 changes: 12 additions & 20 deletions book/src/move-basics/primitive-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ Where:
- `<expression>` - the value to be assigned to the variable

```move
let x: bool = true;
let mut y: u8 = 42;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:variables_and_assignment}}
```

A mutable variable can be reassigned using the `=` operator.
Expand All @@ -42,8 +41,7 @@ y = 43;
Variables can also be shadowed by re-declaring.

```move
let x: u8 = 42;
let x: u8 = 43;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:shadowing}}
```

## Booleans
Expand All @@ -53,8 +51,7 @@ The `bool` type represents a boolean value - yes or no, true or false. It has tw
the type - the compiler can infer it from the value.

```move
let x = true;
let y = false;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:boolean}}
```

Booleans are often used to store flags and to control the flow of the program. Please, refer to the
Expand All @@ -72,10 +69,7 @@ Move supports unsigned integers of various sizes: from 8-bit to 256-bit. The int
- `u256` - 256-bit

```move
let x: u8 = 42;
let y: u16 = 42;
// ...
let z: u256 = 42;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integers}}
```

Unlike booleans, integer types need to be inferred. In most of the cases, the compiler will infer
Expand All @@ -84,9 +78,7 @@ infer the type and will require an explicit type annotation. It can either be pr
assignment or by using a type suffix.

```move
// Both are equivalent
let x: u8 = 42;
let x = 42u8;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integer_explicit_type}}
```

### Operations
Expand All @@ -102,6 +94,9 @@ multiplication, division, and remainder. The syntax for these operations is:
| % | modular division | The divisor is 0 |
| / | truncating division | The divisor is 0 |

> For more operations, including bitwise operations, please refer to the
> [Move Reference](/reference/primitive-types/integers.html#bitwise).
The type of the operands _must match_, otherwise, the compiler will raise an error. The result of
the operation will be of the same type as the operands. To perform operations on different types,
the operands need to be cast to the same type.
Expand All @@ -114,22 +109,19 @@ the operands need to be cast to the same type.
Move supports explicit casting between integer types. The syntax for it is:

```move
(<expression> as <type>)
<expression> as <type>
```

Note, that it requires parentheses around the expression to prevent ambiguity.
Note, that it may require parentheses around the expression to prevent ambiguity.

```move
let x: u8 = 42;
let y: u16 = (x as u16);
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:cast_as}}
```

A more complex example, preventing overflow:

```move
let x: u8 = 255;
let y: u8 = 255;
let z: u16 = (x as u16) + ((y as u16) * 2);
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:overflow}}
```

### Overflow
Expand Down
43 changes: 43 additions & 0 deletions packages/samples/sources/move-basics/primitive-types.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,47 @@

module book::primitive_types {

#[test, allow(unused_variable, unused_let_mut)]
fun test_primitive_types() {

// ANCHOR: variables_and_assignment
let x: bool = true;
let mut y: u8 = 42;
// ANCHOR_END: variables_and_assignment

// ANCHOR: shadowing
let x: u8 = 42;
let x: u16 = 42;
// ANCHOR_END: shadowing

// ANCHOR: boolean
let x = true;
let y = false;
// ANCHOR_END: boolean

// ANCHOR: integers
let x: u8 = 42;
let y: u16 = 42;
// ...
let z: u256 = 42;
// ANCHOR_END: integers

// ANCHOR: integer_explicit_type
// Both are equivalent
let x: u8 = 42;
let x = 42u8;
// ANCHOR_END: integer_explicit_type

// ANCHOR: cast_as
let x: u8 = 42;
let y: u16 = x as u16;
let z = 2 * (x as u16); // ambiguous, requires parentheses
// ANCHOR_END: cast_as

// ANCHOR: overflow
let x: u8 = 255;
let y: u8 = 255;
let z: u16 = (x as u16) + ((y as u16) * 2);
// ANCHOR_END: overflow
}
}

0 comments on commit 39c89be

Please sign in to comment.