Thirty Days of Rust: Day One

Sammy Shear - Jan 10 '22 - - Dev Community

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.

. . . . . . . .
Terabox Video Player