Skip to content

Commit

Permalink
Merge pull request #3422 from LXIF/patch-3
Browse files Browse the repository at this point in the history
Update counter.mdx to use current imports, small bugfix
  • Loading branch information
jessiemongeon1 authored Sep 4, 2024
2 parents 96c173a + 84b9a29 commit 6c3ee0e
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions docs/developer-docs/backend/rust/counter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,55 +49,54 @@ Now that you have the files in place for your Rust dapp, you can replace the tem
To replace the default dapp, open the template `src/rust_counter_backend/src/lib.rs` file in a text editor and delete the existing content. Then, copy and paste this code into the file:

```rust title="src/rust_counter_backend/src/lib.rs"
use std::cell::RefCell;
use candid::types::number::Nat;
use ic_cdk::{query, update};
use std::cell::RefCell;

thread_local! {
    static COUNTER: RefCell<Nat> = RefCell::new(Nat::from(0));
static COUNTER: RefCell<Nat> = RefCell::new(Nat::from(0 as u32));
}

/// Get the value of the counter.
#[ic_cdk_macros::query]
#[query]
fn get() -> Nat {
    COUNTER.with(|counter| (*counter.borrow()).clone())
COUNTER.with(|counter| counter.borrow().clone())
}

/// Set the value of the counter.
#[ic_cdk_macros::update]
#[update]
fn set(n: Nat) {
    // COUNTER.replace(n);  // requires #![feature(local_key_cell_methods)]
    COUNTER.with(|count| *count.borrow_mut() = n);
COUNTER.with(|counter| *counter.borrow_mut() = n);
}

/// Increment the value of the counter.
#[ic_cdk_macros::update]
#[update]
fn increment() {
    COUNTER.with(|counter| *counter.borrow_mut() += 1);
COUNTER.with(|counter| *counter.borrow_mut() += 1 as u32);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_set() {
        let expected = Nat::from(42);
        set(expected.clone());
        assert_eq!(get(), expected);
    }

    #[test]
    fn test_init() {
        assert_eq!(get(), Nat::from(0));
    }

    #[test]
    fn test_inc() {
        for i in 1..10 {
            inc();
            assert_eq!(get(), Nat::from(i));
        }
    }
use super::*;

#[test]
fn test_get_set() {
let expected = Nat::from(42 as u32);
set(expected.clone());
assert_eq!(get(), expected);
}

#[test]
fn test_init() {
assert_eq!(get(), Nat::from(0 as u32));
}

#[test]
fn test_inc() {
for i in 1..10 {
increment();
assert_eq!(get(), Nat::from(i as u32));
}
}
}
```

Expand Down Expand Up @@ -137,8 +136,8 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
candid = "0.8.2"
ic-cdk = "0.7.0"
candid = "0.10"
ic-cdk = "0.13"
serde = { version = "1.0", features = ["derive"] }
ic-cdk-macros = "0.8.0"
```
Expand Down

0 comments on commit 6c3ee0e

Please sign in to comment.