DEV Community

Cover image for First Week learning rust
Enyel Sequeira
Enyel Sequeira

Posted on • Edited on

First Week learning rust

My First week learning Rust using (The Rust Programming Language) "The Book"

Why Rust?

  • As a Front-End developer, there is a lot you need to learn and keep up to date. One of the biggest changes coming to web development is web3.0, if you are not living under a rock you have probably heard about it, at least a little. With these changes coming to the web I decided to learn rust to make myself more valuable, this decision also comes after researching the kind of projects you could do. One of them is a compiler, which funny enough my favorite React framework Next uses. I am a big fan of Next and my goal is to understand how rust made a framework that was already fast even faster.

My Plan

My First-week Impressions

  • Coming from a JavaScript background, I had quite a few impressions. Most of them were good.
  • To begin, the first thing I noticed like most people, is that it is a static language, but what does that mean?. To put it simply, if you have been doing JavaScript for a while most likely you have started working with TypeScript and you noticed that you have to type most of your code. rust is the same way. You can either love this or hate it, for me I loved this, while I was trying some code it made me realize that you can code and deploy a lot faster if the language is typed this also cause fewer mistakes when tracking errors and deploying code.
  • My first week of learning rust I noticed how fast it is, I mean really fast. Creating a project was extremely fast taking less than 5s to get a project set up, granted it doesn't install any dependencies which I learned are called crates in rust.

Things I have learned this week.

  • By default when declaring a variable that becomes immutable, however, it is possible to make it mutable by adding the mut keyword like so let mut number
  • You can also declare a variable using the const keyword, gotcha with this is that constants can not be assigned to a return value of a function.
  • Declaring a function is similar to other languages you declare it using fn keyword and the entry point of any application is the fn main(){...code}
  • The way printing to the terminal works is also different in rust. In JavaScript we can interpolate, but in rust not yet, although it was mentioned that this is a feature that they want to add in the next release.
    • let mut number: i32 = 50
    • println!("number is {}", number);
  • This will print the number 50 to the terminal, it reads the variable after the comma.
  • Semicolons are really important in rust it can be the difference between a statement and expression and yes they are different

    • a Statement is an action that does not return a value
    • Expression evaluates something and returns a value
  • Types, in rust there are 2 data types subsets

    • Scalar

      • integers
        • just like numbers and we can use them signed and unsigned. If it needs a sign before the number, it should be typed using the signed type. This is used for negative numbers. Intergers chart
      • floating-points
        • it has two types and the default is f64
          • f32 ==32bits==
          • f64 ==64bits==
      • Booleans
        • True/False
      • Characters
    • Compound

      • Tupples
        • If you have done any TypeScript this works in a similar way
        • let tup: (i32, f64, u8) = (500, 6,4, 1)
        • We can destructure like let (x, y, z) = tup;
        • and get the values using indexes
      • Arrays
        • Arrays are a bit tricky at least to my understanding all of the array items must be of the same type. They also have a fixed length and cannot add or remove. Instead, it was mentioned if I need to add or remove to use something like a Vector (still not sure what this is)
        • Accessing the element is the same way as it is in JavaScript
    • Maybe more, but still haven't learned about them.

  • Control flow works the same way as it does in any other language using if and else if

  • You have 3 ways to loop (that I have read),

    • loop
    • while
    • for
  • Functions can have an explicit return and with this, it does not matter where the function is declared, you can declare it at the end of the file and call it, in the beginning, this will still work. Being that is a static typed language you need to type the parameters of a function.

functions explicit

  • Explicit return of the function five

function semicolons

  • In rust like mentioned above it is important the use of semicolons explicit
    • this would not work because if we add a semicolon at the end of x+1; fn plus_one function this makes it into a statement and if you remember a statement does not have a return value.

Conclusion

While I have learned a bit more than just these topics, I believe these are the key concepts of what I have learned this week. I will continue to write what I have learned from the book each week and share it, if you want to follow you can follow my socials.

I am a beginner in the rust language so please keep in mind that the information given here is just my understanding and might not be the correct way. If you do see a typo or misinformation please do leave a comment. πŸ˜ƒ

PS: If you are interested in doing a Bootcamp, I have partnered with Practicum By Yandex to offer a discount. This is one of the boot camps I did, and I really enjoyed the process. It offers a 30% discount which is pretty sweet considering most boot camps are expensive.

Top comments (14)

Collapse
 
peerreynders profile image
peerreynders

By default when declaring a variable that becomes immutable, however, it is possible to make it mutable by adding the mut keyword like so let mut number.

In some ways this perspective β€” fuelled by the chosen syntax β€” is bound to create confusion later.

When it comes to Rust train your brain ASAP to make the following automatic substitutions:

  • immutable -> shared access
  • mutable -> exclusive access (unique)

i.e. "shared" values cannot be mutated and "exclusive" values cannot be "shared".

The immutable ⟷ mutable tension is only a consequence of the primary shared ⟷ exclusive tension.

By default values are assumed to have shared access and therefore have to be explicitly marked for exclusive access (mut).

See

Collapse
 
enyelsequeira profile image
Enyel Sequeira

Nice thank you the tips, specially that link about ownership is a lot at first

Collapse
 
deciduously profile image
Ben Lovy

You're just in time, implicit named capture is already available in nightly/unstable! format!(" Hello {name}!"); is indeed coming to stable Rust really soon.

Collapse
 
lexiebkm profile image
Alexander B.K.

I have build a very simple, console hello world program using Rust, Go and C++ on Windows. The executable file size is surprising to me :
Rust is 4Mb, Go is about 3Mb, while C++ is only 171kb (as expected). For C++ I use g++ compiler. I installed Rust using its GNU version of Msi installer.
Can you explain why ?
When I have developed a relatively large web app using PHP+Laravel in backend as well as React, Bootstrap and other 3rd party libs in frontend that has a lot of forms, the bundle size was about 8Mb which I thought was very big. I couldn't use code-splitting at that time.
Now, seeing the 3-4Mb for a console hello world that basically only has one printLn using both Rust and Go seems to be unacceptable.

Collapse
 
deciduously profile image
Ben Lovy

I don't know about Go, so I can't speak for that. With Rust, you're seeing these sizes due to static linking, and symbols being included in the binary. On Linux, a hello-world compiled with --release generates a 3.7M executable. Just running strip on this executable reduces the size to 310k. You can also se size-related optimizations in your profile:

[profile.release]
opt-level = "z"
lto = true
Enter fullscreen mode Exit fullscreen mode

Other tweakables include panic behavior - you can remove the unwinding mechanism using this:

[profile.release]
panic = "abort"
Enter fullscreen mode Exit fullscreen mode

So, yes, while the defaults produce some hefty executables, Rust does expose tools to more finely tweak your result. This repo provides a much more comprehensive overview.

Thread Thread
 
lexiebkm profile image
Alexander B.K.

Thanks for the answer. This is just a start for my journey of Rust. I have a plan to try it for web backend after reading your post about it. We know that the book also has a chapter on web server that I can use for starting point.

Collapse
 
peerreynders profile image
peerreynders • Edited

The executable file size is surprising to me : Rust is 4Mb …

Quote

The executable is 3.5 MiB but there's only 212 KiB of actual executable code, the rest is debug symbols.

Minimizing Rust Binary Size

With C/C++ knowledge Programming Rust 2e may be a better starting point β€” though if that knowledge is spotty, it could still become a hard slog.


TinyGo - Go compiler for small places


FYI: Zig

Thread Thread
 
lexiebkm profile image
Alexander B.K.

Thanks for the explanation, sir.

Collapse
 
lexiebkm profile image
Alexander B.K.

It is interesting how Rust implements OOP, specifically how it defines a method. Now it reminds me of Go, being a non-OOP language, how it defines a method using a receiver argument which can be a pointer.
Rust is not a fully OOP or even not OOP when using certain definition, unlike C++, Java or C# which are fully OOP. But I like when it provides Struct that we have known in C/C++ and also find in Go. And it provides pointer and smart pointer too. This is why I think, if I have time, I can learn Rust, Go and C++ (relearn) in the same week. :)

Collapse
 
enyelsequeira profile image
Enyel Sequeira

Yes, I was reading about it, it will be cool, makes it more inuitive πŸ˜ƒ

Collapse
 
lexiebkm profile image
Alexander B.K.

When I read the book the 1s time, one thing I wanted to know whether Rust provided similar kind of pointers that C/C++ had, or how it managed memory. Then I found one new concept : Ownership which was interesting. But I stopped reading the documentation/book because of other priorities, knowing that it would take long time to learn.
Whenever I think to learn Rust, though, I also remember C++ which I want to re-learn in the correct way using Stanley Lippman's book (C++ Primer).
Ideally, I want to manage my time to learn Rust and C++. I have installed GCC compiler for writing C++ code. I expect I can install Rust which can use GNU compiler instead of MSVC.

Collapse
 
enyelsequeira profile image
Enyel Sequeira

I am actuall at the part with owneship, and I found it a bit confusing, since this is my first time learning a low level language, I never really had to think about these factors. But it is definetely interesting to learn about it, I have seen that just with everything theres a trade-off though

Collapse
 
sionkim00 profile image
sionkim00

Hi Enyel, can you share Practicum discount code with me? I'm interested in their web dev course. Thank you!

Collapse
 
enyelsequeira profile image
Enyel Sequeira

Hello, yes. Just saw your message if you have twitter please message me there enyelsequeira