DEV Community

Cover image for First Week learning rust

First Week learning rust

Enyel Sequeira on November 22, 2021

My First week learning Rust using (The Rust Programming Language) "The Book" Why Rust? As a Front-End developer, there is a ...
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