diff --git a/modules/developers-guide/examples/lib.rs b/modules/developers-guide/examples/lib.rs new file mode 100644 index 000000000..5f9358b66 --- /dev/null +++ b/modules/developers-guide/examples/lib.rs @@ -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; + } +} diff --git a/modules/developers-guide/pages/tutorials/counter-tutorial.adoc b/modules/developers-guide/pages/tutorials/counter-tutorial.adoc index 9e870f3f5..839e365f2 100644 --- a/modules/developers-guide/pages/tutorials/counter-tutorial.adoc +++ b/modules/developers-guide/pages/tutorials/counter-tutorial.adoc @@ -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