7

Using Rust Playground for Hello World and Variable Interpolation

 3 years ago
source link: https://fuzzyblog.io/blog/rust/2020/03/02/using-rust-playground-for-hello-world-and-variable-interpolation.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using Rust Playground for Hello World and Variable Interpolation

Mar 2, 2020

IMG_1313.jpg

Artwork by my friend Autumn Mott; Hopefully I can find a better link to put here

It is a Monday and what better way to start your 6 am Monday morning then learning some of the elements of a new language – Rust.

I started by adding a link in my Browser toolbar to the Rust Playground which amounts to a web based REPL (Read Evaluate Print Loop) for Rust where you can type in Rust code and run it. Yes I know it really isn't a REPL because Rust is a compiled language not an interpreted one but it functions well enough as a REPL that I can wrap my Ruby tinged mind around it.

Here's the Hello World program that automatically appears in the Rust playground

fn main() {
    println!("Hello, world!");
}

The output of this is:

Hello, world!

That's pretty easy to understand:

  • A main function defined with fn
  • A print line function defined with a ! (my previous Rust reading tells me that's a macro indicator)
  • { and } to denote structure
  • A ; to denote the end of lines

I wanted to make a simple change to experiment with the use of variables so I added a main2() function and called it from main():

fn main() {
    println!("Hello, world!");
    main2();
}

fn main2() {
    let x = 5;
    println!("The value of x is: {}", x);
    let y = 6;
    println!("The value of y is: {}", y);
}

The output of this is:

Hello, world!
The value of x is: 5
The value of y is: 6

You can see that the let keyword assigns a variable and that {} binds a variable into a string (which is generally called interpolation).

Note: Variables quickly bring you in to the heart of Rust – immutability – and here there by dragons that hopefully come up tomorrow after some reading.

Here's a permanent link to this if you want to try it out.

What about Repl.it?

Another way to have a web based REPL for Rust is Repl.it. And while I like the concept of repl.it, they have eliminated any privacy without a paid account: Upgrade your account for private repls. This appears on the bottom of every new REPL you create and at $74 / 12 months that feels expensive simply for the basics of privacy.


Posted In: #rust #variables


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK