Lesson 1 of 9 · 2 min 7 sec

Quickstart: Follow One Call Through the App

Chapters

  1. Find the three kinds of file
  2. Save one score
  3. Read it back
  4. Check it without a canister
Transcript
Download transcript

Find the three kinds of file

This is Quickstart, a complete canister that saves scores. Its public methods are in lib dot r s. The scores feature has its own folder. The root folder wires the app together at startup. Inside a feature folder, mod dot r s holds the service, which is the code other parts of the app call. The memory folder declares the data that the feature saves. That declaration has a version in its file name.

Save one score

A client calls put with player nine and score forty-two. The update attribute makes this function a public method. Its guard lets only a controller call it. The body asks the root for write access and calls the scores service. The service method opens its own saved data for writing and inserts the score into a map. The field is visible only inside the scores module. An endpoint cannot reach it, and neither can another feature. They call put.

Read it back

Reading works the same way with one difference. A query calls root read. The service method copies the value out and returns it. The client receives Some forty-two. Read methods are written for any kind of access. Write methods exist only under a write scope. Quickstart includes a test that tries to call put from a read scope, and it must fail to compile.

Check it without a canister

The same service runs in an ordinary Rust test. The test installs the app, saves two scores in one write scope, and reads the first one back. Then the test drops the app. The service objects are gone, and the saved scores are still there. Opening the app again builds new services over the same data. The count is two. Removing player nine works once, and player seventeen still has thirteen. Every stateful feature in an app follows this shape. Next we add one to a larger app, starting from an empty folder.