Skip to content
This repository was archived by the owner on May 23, 2022. It is now read-only.
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions modules/developers-guide/examples/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use ic_cdk_macros::*;

static mut COUNTER: u32 = 0;

#[query]
fn read() -> u32 {
unsafe {
ic_cdk::print(format!("read {}", COUNTER));
COUNTER
}
}

#[update]
fn inc() -> () {
unsafe {
ic_cdk::print(format!("inc {} + 1", COUNTER));
COUNTER = COUNTER + 1;
}
}

#[update]
fn write(input: u32) -> () {
unsafe {
ic_cdk::print(format!("write {} := {}", COUNTER, input));
COUNTER = input;
}
}
13 changes: 10 additions & 3 deletions modules/developers-guide/pages/tutorials/counter-tutorial.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,20 @@ To modify the default template source code:
. Open the `+src/my_counter/increment_counter.mo+` file in a text editor and delete the existing content.
. Copy and paste the following sample code into the `+increment_counter.mo+` file:
+
[source,motoko]
[tabs]
====
Motoko::
+
[source,motoko,indent=0]
----
include::example$counter.mo[]
----
Rust::
+
Note that the variable declaration in this example includes the `+stable+` keyword to indicate the state—the value that can be set, incremented, and retrieved—persists.
For more information about stable and flexible variables, see the _{proglang} Programming Language Guide_.
[source,rust]
----
include::example$lib.rs[]
----
. Save your changes and close the file to continue.

== Start the local network
Expand Down