Skip to content

Commit

Permalink
Some cleanup to the docs (FuelLabs#1672)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadfawaz authored May 25, 2022
1 parent cbc5c68 commit 09e2ed4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 32 deletions.
10 changes: 3 additions & 7 deletions docs/src/basics/control_flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ fn main() {
// do something else
} else {
// do something else
}; // <------------ note this semicolon
}
}
```

In Sway, note that a _statement_ is a _declaration **or** expression with a semicolon after it_. This means that you need to add a semicolon after an `if` to turn it into a statement, if it is being used for control flow.

This need for a semicolon after if expressions to turn them into statements may be removed eventually.

### Using `if` in a `let` statement

Like Rust, `if`s are expressions in Sway. What this means is you can use `if` expressions on the right side of a `let` statement to assign the outcome to a variable.
Expand Down Expand Up @@ -60,7 +56,7 @@ You need the `while` keyword, some condition (`value < 10` in this case) which w

There are no `break` or `continue` keywords yet, but [they're coming](https://github.com/FuelLabs/sway/issues/587).

For now, the way to break out of a `while` loop early is to manually invalidate the condition. In this case, that just means setting `counter` to be >= 10.
For now, the way to break out of a `while` loop early is to manually invalidate the condition. In this case, that just means setting `counter` to be `>= 10`.

Building on the previous example, here's what that might look like:

Expand All @@ -75,7 +71,7 @@ while counter < 10 {
// calling some other function to set the bool value
break_early = get_bool_value();
counter = counter + 1;
};
}
}
```

Expand Down
20 changes: 0 additions & 20 deletions docs/src/reference/rust_differences.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,3 @@ struct MyStruct {
field_two: bool,
}
```

## If Expressions

In Sway, a _statement_ is a _declaration **or** expression with a semicolon after it_. This means that you need to add a semicolon after an `if` to turn it into a statement, if it is being used for control flow:

```sway
fn main() {
let number = 6;
if number % 4 == 0 {
// do something
} else if number % 3 == 0 {
// do something else
} else {
// do something else
}; // <------------ note this semicolon
}
```

This need for a semicolon after if expressions to turn them into statements will be removed eventually, but it hasn't been removed yet.
2 changes: 1 addition & 1 deletion examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MyOwnedContract for Contract {
assert(addr.into() == OWNER);
} else {
revert(0);
};
}

true
}
Expand Down
2 changes: 1 addition & 1 deletion examples/signatures/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ fn main() {
log(address.value);
} else {
revert(0);
};
}
}
6 changes: 3 additions & 3 deletions examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ abi Wallet {

impl Wallet for Contract {
fn receive_funds() {
if (msg_asset_id()).into() == NATIVE_ASSET_ID {
if msg_asset_id().into() == NATIVE_ASSET_ID {
storage.balance = storage.balance + msg_amount();
};
}
}

fn send_funds(amount_to_send: u64, recipient_address: Address) {
Expand All @@ -36,7 +36,7 @@ impl Wallet for Contract {
assert(addr.into() == OWNER_ADDRESS);
} else {
revert(0);
};
}

let current_balance = storage.balance;
assert(current_balance > amount_to_send);
Expand Down

0 comments on commit 09e2ed4

Please sign in to comment.