My First Rust Program

I’ve had a Rust sticker on my laptop for over a year now. I thought perhaps I should finally learn the langauge.

My First Rust Program

I’ve had a Rust sticker on my laptop for over a year now. I thought perhaps I should finally learn the langauge.

Note: The project is available here.

Rust has been declared as the most loved programming language consecutively for the past four years in the StackOverflow Developer Survey. I myself marked Rust as one of the languages I’d like to work with in the future, in the 2020 Survey. And due to the COVID-19 vacations, I finally got the time to try it out.

So I installed Rust and read a few pages of Rust by Example. Then I came across this problem about a game of coins with the Devil, and decided to implement the game as my first project in Rust.

I believe in learning languages the hard way. I learnt Java by experimenting. I used to copy code that I didn’t understand, then modified small parts, observed the changes, and repeated this until I understood the entire code. And I learnt python and C++ by by using them to re-write the same programs that I wrote in Java. This also taught me how to use Google properly. I learnt Go by deciding that I’d use it to build my best self project yet (an online election system), when I hardly knew how to write Hello World in it; and I learnt it not by following some guide, but by searching for “how to do X in Go”.

So why should Rust be an exception? I created a new cargo project and started modifying it. I really liked that creating a new project is as simple as doing cargo new project-name.

The first thing that I had to do was to take an integer as an input. I was awfully disappointed that there is no direct way to take an integer input: we first have to input a string and then parse it into an integer.

Java, at one point, used to be my favourite language. And Java libraries use the camelCase/PascalCase for all naming purposes (except for CONSTANTS). Now Go is my favourite language. And Go too uses a similar convention. Therefore, all my life, I’ve preferred using the camelCase. And now I’m writing code in a language that gives me a warning, asking me to convert camelCase variable names to snake_case. First turn-off. :p

When the input part was ready, I ran it using cargo run. It seemed to take infinity to compile - when I realized that it was in fact waiting for the input. However, I had print!ed a string before asking for the input, which was not displayed. Flushing problem, I recognized. Changing print! to println! did the work, but I still wanted the input to be taken on the same line as the prompt. So I had to add another statement to flush - a rather scary looking statement that required a new Trait to be imported. I didn’t really like having to manually flush after print!ing, but there has been a huge discussion over this and the community believes this is the way it should be. :(

For the next step, I had to create an array of the size that I had just inputted. The error messages told me that I could not use a variable for the size of the array - it had to be a constant. LoL what? I then found that the way to do it is by using a vector. Fair enough. Then I realized that an i32 value could not be used as an array/vector index; it had to be a usize. Reasonable, since array indices should be positive. However, I believe an automatic type conversion should have been implemented (again, the subject of a huge discussion).

I then had to randomly initialize the vector. Turns out, I have to add an external crate as a dependency to generate random numbers. Seriously? In my opinion, random number generation is something as fundamental as I/O and should have been present in the standard library of Rust.

At this point, I had a randomly initialized array. I now needed to update it using inputs from the user. I was about to face the most dreaded error any Rust beginner could face : error[E0382]: use of moved value. The concept of this error doesn’t even exist in other languages. So I modified the vector in an iteration of the while loop and now I can’t use the vector in the while condition? LMAO why? It took me an entire day to figure it out, and I have probably not yet properly understood it. I somehow managed to solve it, but I still don’t know if the way I did it is the way it was supposed to be done. I’ll need to understand the idea of ownership before I pick up any other project in Rust.

So that was it - an utterly disappointing experience. I once overheard yashsriv saying that anyone denigrating Rust online would be hurled abuses at. So perhaps I should brace for that. The program is available on GitHub at dryairship/coins-and-the-devil. Do point out any mistakes by creating a GitHub issue or in the comments here.

Avatar
Priydarshi Singh
Software Engineer

Breaker · Builder · Engineer

comments powered by Disqus