DEV Community

Sammy Shear
Sammy Shear

Posted on • Edited on

Thirty Days of Rust: Day One

I'll admit it, I've never really used Rust. It's interested me for a while, but I never gave myself an excuse to use it until I had this idea. It's not very original by any stretch of the imagination to do this kind of thing, but this is how I figured I'd get myself to do this. The first day is just going to be a simple setup of my environment. I'll be using VSCode as my editor, rustup as my version manager, and of course, cargo as my package manager.

Rustup

I'm not going to lie, to find rustup I literally just looked up "Rust Version Manager" and chose it over rsvm because it seemed to have more documentation. That being said, to install it I just used the curl command from https://rustup.rs, ran it, and then immediately tried to use the rustup command, which didn't work because rustup did not automatically add itself to my zshrc path. I did that manually (the binary is at ~/.cargo/bin), and the command worked just fine.

Cargo

Cargo works a lot like basically every other package manager, whether it's npm, dotnet, or pip (although pip does much less than the rest). It allows you to install crates through https://crates.io, but like npm or dotnet, it also allows you to initialize a project and run said project. To start my project, I ran cargo new day1, and opened up the directory in VSCode.

VSCode

There's an extension pack for VSCode that adds the features you're going to want from Rust programming, including a language server, a nice Cargo.toml (similar to package.json) helper, and better toml language support. You can swap out the language server for the alternative rust-analyzer extension, but I went with the main language server.

App

You'll notice if you're following along with me that your new rust project already has a hello world program set up for you, but to explore the features of rust I tweaked it a bit.

// src/main.rs

use rand::seq::SliceRandom;
use rand::thread_rng;
use std::str;

fn main() {
    let mut rng = thread_rng();
    let mut string_to_shuffle: Vec<char> = "Hello, World!".chars().collect();
    let unshuffled: &str = &string_to_shuffle.iter().collect::<String>();
    println!("Unshuffled: {}", unshuffled);
    string_to_shuffle.shuffle(&mut rng);
    let shuffled: &str = &string_to_shuffle.iter().collect::<String>();
    println!("Shuffled: {}", shuffled);
}
Enter fullscreen mode Exit fullscreen mode

This was a really stupid way of getting from "Hello, World!" to complete gibberish, but I learned about converting from strings to vectors and back again, so I consider it worth it.

$ cargo run
Unshuffled: Hello, World!
Shuffled: o! WHllo,rdel
Enter fullscreen mode Exit fullscreen mode

I like Rust, I'll say it. I realize that's a very brave position to take because it's clearly as hated as PHP, but I like Rust and I look forward to the rest of these days. It's definitely going to be a challenge, even more than I would've thought, to do this, but I'm excited for it. I also might do some bigger projects over the course of multiple days later on in this challenge.

Top comments (5)

Collapse
 
liftoffstudios profile image
Liftoff Studios

Dude Rust is not hated, it is loved by devs 😆

Collapse
 
sammyshear profile image
Sammy Shear

Yeah I know, I was trying to be sarcastic there 😂

Collapse
 
chayimfriedman2 profile image
Chayim Friedman

rustup is a bit more than "better maintained", it is an official project of Rust.

And rsvm? Well, never heard of it up to this moment...

Another thing, I would really recommend you to try rust-analyzer out. The Rust extension is very slow and laggy, while rust-analyzer is excellent. It is also semi-official and supposed to replace the Rust extension at some point in the future.

The official extension just compiles your whole project on every keystroke and decrypts a big ol' chunk of json the compiler gives back; rust-analyzer, on the other hand, incrementally parses & analyzes your code, and it is going rapidly better.

Collapse
 
sammyshear profile image
Sammy Shear

Thank you for the rust-analyzer recommendation! If you've never heard of rsvm then I'm glad I chose rustup, but rsvm is just the first result on google when you look it up, so I figured either was okay. I didn't notice the "official rust project" part of rustup until day 2, but I figured it wasn't super important to bring up again.

Collapse
 
taikedz profile image
Tai Kedzierski

that's a very brave position to take because it's clearly as hated as PHP

Is it? Where are you seeing that? I've seen only love and praise for it so far... and if Stack Overflow's yearly polls are anything to go by, it's clearly very sought after... To the extent that Linux kernel module maintainers (bunch of very opinionated C people) are looking to leverage it...

Other than that, thanks for the post, I've been wanting to get a start on this language too. I'll be following keenly 😉