ib RustBite Code Experiments
about
05/19/2022
RustBites - Code Experiments
Code Repository Code Examples

Rust Bite - Code Experiments

getting started with Rust code experimentation

1.0  Introduction

Figure 1. Rust Playground
Figure 2. Playground Screen Scrape
Figure 3. Playground PermLink
Figure 4. CodeExperiments with Cargo
Figure 5. CodeExperiments with Vs Code
The Rust Playground is a great tool for experimenting with Rust Code. It has a web interface, backed by a Rust Compiler that provides all of the usual error messages and other help like clippy, assem output, ... You can select code from any of the pages in this or other sites, copy to the clipboard (ctrl-c) and paste into the playground (ctrl-v). That works as shown by the running example in Figure 2. taken from the RustBites_Options.html page. That allows you to experiment with code by making changes, seeing compiler errors, fixing, and running. You can do the same thing with the Playground examples provided in Start discussed during BuildOn #1 presentation.

2.0  Saving Experiments and Porting

Once you start experimenting in the playground you will probably want to save your work.

Saving Permlinks

If you have some code you want to save, the easiest way is to click the SHARE button at the top right of the playground screen. That gives you a screen like Figure 3. Click on the "Permalink to the Playground" link. When it comes back you then click on the clipboard icon. That will place a link on your clipboard to a saved version of your code. Note that if you leave the playground, perhaps by using the back button, you will loose your code, so saving occasionally is a good idea. Now you have to save and label the link to your code. You can do that in Windows OneNote. Also, you can save to a file using Visual Studio Code:
  1. Open VS Code and navigate to a directory where you want to store a file of Permalinks.
  2. Create a file, using the VSCode File menu, "PlaygroundLinks.txt" perhaps.
  3. Now you can just paste labels and links, using the VSCode editor, into that file.
  4. Retrieving code is simple. Just click one of the links. That will open Playground with your code pasted into the left screen.

Porting to Rust source file

If you are working on code with some lasting value you may wish to port it to a Rust source file. That is easy enough to do if you know how the Rust tool Cargo works.
  1. Open the cmd application, navigate to a directory where you want to run Rust code.
  2. Emit the command:
    cargo new code_experiments --lib
    That builds a Rust library project. You aren't going to build any library code, but that allows cargo to build and run one of many different executables.
    Cargo uses this structure to run one or more applications that use library code by emitting the command:
    cargo run --example test1
    We are going to do the same thing except there is no library and the executables don't depend on anything in the /src directory where cargo puts library code.
    Note that the name of the directory where you store your code experiments is "examples" - note it's plural - and the cargo command uses a singular form of that. Cargo uses naming conventions, so if you don't use this convention it won't work.
  3. You see in Figure 5. Visual Studio code running the same code example scraped from the RustBites_Options page. That was created by the process outlined here.
  4. You can do this as many times as you like, hosting many Rust source files with main functions, in the same /examples directory as long as they have different names. You run as many as you like from this one directory.

3.0  Rust Story Examples

When I wrote the Rust Story I had not yet used the Rust Playground. So you will find a number of demo programs in the RustBasicDemos repository. That code would be much easier for you to look at and use if I had followed the process laid out here. I will probably go back and set things up that way, with a set of Playground links on a repository documentation page. But that will have to wait for awhile.

3.0  Epilogue

The Rust Playground tool makes it very easy to experiment with code syntax and some structuring ideas as long as the code isn't too large. The results of your experiments can be saved to a page of links or to an /examples directory holding many *.rs files, each with a main function. You create the subdirectory in the cargo project directory, e.g., the one with the cargo.toml file. Expand Figure 5. to see the details in the left pane. You can then build and run any of the examples using cargo, as shown in Figure 4., or with VS Code, as shown in Figure 5.
  Next Prev Pages Sections About Keys