From fac248bbf314e8a03eb793df6ff0166867f2b60d Mon Sep 17 00:00:00 2001 From: lsgunnlsgunn Date: Wed, 3 Jun 2020 15:48:25 -0700 Subject: [PATCH 1/2] Add Rust counter program and tutorial with tabs --- .../pages/tutorials/counter-tutorial.adoc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/developers-guide/pages/tutorials/counter-tutorial.adoc b/modules/developers-guide/pages/tutorials/counter-tutorial.adoc index 97a4a5662..8b33dcc9d 100644 --- a/modules/developers-guide/pages/tutorials/counter-tutorial.adoc +++ b/modules/developers-guide/pages/tutorials/counter-tutorial.adoc @@ -149,12 +149,23 @@ To modify the default template source code: cd src/increment_counter ---- . Open the template `+main.mo+` file in a text editor and delete the existing content. -. Copy and paste the following sample code into the `+main.mo+` file: +. Click the link to the programming language you want to use, then copy and paste the sample code into the main program file: + -[source,motoko] +[tabs] +==== +Motoko:: ++ +[source,motoko,indent=0] ---- include::example$counter.mo[] ---- +Rust:: ++ +[source,rust] +---- +include::example$lib.rs[] +---- +==== . Save your changes and close the file to continue. == Build and deploy the program From 8970e0fb51e67034fa8057d3e1f785011b1051cf Mon Sep 17 00:00:00 2001 From: lsgunnlsgunn Date: Thu, 4 Jun 2020 13:34:41 -0700 Subject: [PATCH 2/2] Add main Rust counter program to examples --- modules/developers-guide/examples/lib.rs | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 modules/developers-guide/examples/lib.rs 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; + } +}