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

Fix: Clone who parameter before inserting into balances map #139

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from all 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
8 changes: 4 additions & 4 deletions Rust_State_Machine/en/Section_1/Lesson_3_Store_And_Read.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Before we continue, let's take a moment to go over some Rust which we will be us

### Option and Option Handling

One of the key principals of Rust is to remove undefined behavior from your code.
One of the key principles of Rust is to remove undefined behavior from your code.

One way undefined behavior can happen is by allowing states like `null` to exist. Rust prevents this by having the user explicitly handle all cases, and this is where the creation of the `Option` type comes in. Spend a moment to re-review [the section on `Option`](https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html?highlight=option#the-option-enum-and-its-advantages-over-null-values) from the Rust book if needed.

Expand Down Expand Up @@ -49,7 +49,7 @@ match maybe_value {

> IMPORTANT NOTE!

What you SHOULD NOT do is blindly `unwrap()` options. This will result in a `panic` in your code, which is exactly the kind of thing Rust was designed to prevent! Instead, you should always explicitly handle all of your different logical cases, and if you let Rust do it's job, your code will be super safe.
What you SHOULD NOT do is blindly `unwrap()` options. This will result in a `panic` in your code, which is exactly the kind of thing Rust was designed to prevent! Instead, you should always explicitly handle all of your different logical cases, and if you let Rust do its job, your code will be super safe.

In the context of what we are designing for with the balances module, we have a map which has an arbitrary number of user keys, and their balance values.

Expand Down Expand Up @@ -85,7 +85,7 @@ To make our module useful, we need to at least have some functions which will al
```rust
impl Pallet {
pub fn set_balance(&mut self, who: &String, amount: u128) {
self.balances.insert(who, amount);
self.balances.insert(who.clone(), amount);
}

// -- snip --
Expand All @@ -98,7 +98,7 @@ To make our module useful, we need to at least have some functions which will al

```rust
pub fn balance(&self, who: &String) -> u128 {
*self.balances.get(&who).unwrap_or(&0)
*self.balances.get(who).unwrap_or(&0)
}
```

Expand Down