DEV Community

Cover image for 30 days of Rust - Day 1
johnnylarner
johnnylarner

Posted on • Updated on

30 days of Rust - Day 1

No learning journey starts without printing Hello World! to the console. That's exactly how day 1 went. I learned some CLI basics and was introduced to some basic data types.

Open questions

  • What exactly are the :: and dot operator used for?
  • What is the difference between double and single character boolean operators?
  • Do arrow expressions have additional uses?
  • Do arrow expressions have formatting rules or other kinds of limitations?
  • What on earth are macros in Rust?
  • Are type annotations required or is implicit typing acceptable to the compiler?

Cargo inbound 📦🚢

0 to "Hello World!" in 3 commands

Installing Rust

You can easily install Rust by following the instructions on the official website.

Create your project

Once Rust is installed and added to your PATH, you can run:

cargo new <project_name>
Enter fullscreen mode Exit fullscreen mode

This will add a bunch of boilerplate code as well as good project structure for your code. Two crucial products of your command are the Cargo.lock and Cargo.toml files, which deal with dependency management and reproducibility.

Build and run your project

When in the root of your new project, you can run:

cargo run
Enter fullscreen mode Exit fullscreen mode

This will build and run your project. A fresh project will simply print "Hello World!" to the screen.

More on cargo

The default installation of Rust comes with cargo, Rust's seemingly omnipotent CLI package manager. As we saw above, it is used for project setup and project builds. Here are some other commands:

  • cargo add <package_name> -> add a dependency to the project
  • cargo build -> build the project and yield an executable
  • cargo compile -> compile the project without yielding an executable

At first glance, cargo seems to look a lot like Python's poetry which is a third-party tool. I think I've just scratched the surface of what cargo can do.

To summarise, Rust developers get great set of CLI tools out of the box. Though this are slightly opinionated, it gives junior developers a clearer sense of what "good" project structure can look like.

I do declare

the office I do declare

Variables in Rust can be declared using:

  • let
  • const All variables are by default immutable in Rust, but we can declare a non-constants mutable by adding mut to the declaration:
  • let mut Unused variables should be declared with _

Rust developers use snake case for variable declaration. Lowercase for variables, uppercase for constants.

You are expected (and I believe required) to add type declarations for all variables. VS Code's official Rust extension offers some autocomplete functionality for type declarations.

In summary, Rust has a pretty canonical approach to variables, except for the use and snake case and typing autocomplete in the IDE, which I haven't seen before.

Rusty operators and syntax

how not to spell colon

Here I highlight the most interesting operators and syntax that I've seen in Rust after day 1.

Double colon vs. dot

At the top of most Rust files you'll find import statements. This is an example:

use std::fs::File;
Enter fullscreen mode Exit fullscreen mode

This double colon syntax is very new to my Python eyes. Consider:

from std.fs import File
Enter fullscreen mode Exit fullscreen mode

As Python modules are themselves objects, we can use dot-notation to extract members of those objects. In Rust, it seems that std and fs are crates and mods, respectively, and File is the only object that we can play around with. I presume the double colon operator is used with crates and mods, while the dot operator is reserved for structs. I'll clarify this in the next blog post.

let is_rust_awesome: bool = true;

true and false are Rust's boolean literals. We can use classic boolean operators to compare expressions. What's slightly confusing is that Rust implements both double character and single character operators:

  • && and &
  • || and | I'm not sure what the difference is, this will be answered in the next blog post.

Conditional flows

In Rust, if statements require conditions in parentheses followed by a block wrapped in curly braces. Chained conditionals use the else if keyword with the final clause being marked with the else keyword.

Rust also supports match statements which appear similar to switch statements from other programming languages. A match statement wraps comma-delineated expressions in curly braces.

Each expression is followed by an inline arrow function (expressed through =>) which is executed if that expression evaluates to true. The default case for a match statement is expressed by a _.

I'm intrigued by the arrow function syntax. Currently, it's unclear whether the arrow syntax has rules around formatting or any other kind of limitations as in JavaScript. I'll summarise any rules in the next blog post.

Excel macros in Rust?

To end this blog post, I want to mention the dubious macro type in Rust. A macro is a function with ! in the suffix. Fortunately, I don't think we'll have to do any Excel or VBA courses to understand what's going on here. But I'll be taking a look into this before tomorrow's blog post.

My first impression is that Rust seems to mix and match operators and syntax from C++, JavaScript and Python. Let's see over the coming days whether this first impression somewhat accurate.

Top comments (0)