Rust for TypeScript devs : Borrow Checker

Rust for TypeScript devs : Borrow Checker

ThePrimeagen

1 год назад

213,227 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

ThePrimeagen
ThePrimeagen - 06.12.2022 17:43

Going from TypeScript to Rust can be very difficult. I sure hope you appreciate this video! I was thinking about making more Rust vs Typescript videos. What do you all think? This one is very educational, but i was also going to just do more comparison (not perf wise, but blazingly ergonomic wise).

thoughts? Also, if you like this, SEND THE ALGORITHMIC SIGNALS

Ответить
Oleksii Stri
Oleksii Stri - 16.10.2023 16:47

i kinda get this mutability/immutability a bit, but i still do not grasp the borrow checker thing in a way that i can say i understand it.
but i am only 5 days in rust, building my own app right now, and o boy rust is hard and actix-web is even harder.. gosh.. maybe i will understand borrow checker someday ..or i will accept the notion that i will die dumb :)

Ответить
Jhonatan Jacinto
Jhonatan Jacinto - 09.10.2023 19:24

That was amazing. At the end, this borrow checker thing is not that hard to grasp at all (once you have some experience with programming in other languages, of course).

I also like the idea of explicitly say in the code what is mutable and what is not. That is a plus for Rust indeed. That makes code much clearer and predictable, I believe.

Good job, Prime!

Ответить
Stan Rock
Stan Rock - 03.10.2023 19:18

I'm down for the borrow checker. Doesn't bother me at all. Great video. maybe I subscribe as Rust is new into my toolset. Complete beginner on Rust

Ответить
Stan Rock
Stan Rock - 03.10.2023 19:17

I'm only here because you call mut as mutable and not some freaking dog! 😀

Ответить
Juan Rocamonde
Juan Rocamonde - 01.10.2023 23:34

Great content dude. Your channel is gold. Keep it up.

Ответить
H T Spencer
H T Spencer - 28.09.2023 18:06

God yes, the number of times in languages where I'm like, "hang on, am I passing a reference or the value here? Welp, just gotta find out!" is silly. I love that rust not only makes it clear, it gives you the choice!

Ответить
cparks1000000
cparks1000000 - 25.09.2023 10:33

The easiest way to beat the borrow checker is to write immutible code and pass by reference. Your debugging team (typically consisting completely of future you) will thank you.

Ответить
Das Deck
Das Deck - 21.09.2023 01:21

I guess the explicit mutable is fine, but static memory management seems a bit overkill for what I normally do. I'm content with reference counting, albeit being slightly less safe and at runtime. or am i missing something important here?

Ответить
Or Orsatti
Or Orsatti - 17.09.2023 12:22

Learning c and c++ and it sounds like how shared pointers and unique pointers planned to work but no one thought them through

Ответить
Furz
Furz - 16.09.2023 19:57

putting the &mut infront of the Parameter instead of infornt of the typ is my greatest enemy

Ответить
Chris M
Chris M - 12.09.2023 23:45

I subscribed to ThePrimeagen, and the bug disappeared.
You should post that solution on stack overflow.

Ответить
Randych
Randych - 10.09.2023 20:55

>no garbage collector
Arc and Rc want to know your location

Ответить
jwm0z
jwm0z - 07.09.2023 17:56

Typescript is a scam don’t fall for it js bros

Ответить
Abeltensor
Abeltensor - 05.09.2023 01:34

Nice video. It's on par with my ownership video

Ответить
Kaedenn
Kaedenn - 04.09.2023 22:22

What do you use for your terminal windowing? That doesn't look like any tmux I've seen.

Ответить
Enrico Bottazzi
Enrico Bottazzi - 04.09.2023 20:38

Grazie.

Ответить
Dmitry Kim
Dmitry Kim - 30.08.2023 19:38

Cool think about knowing C++ is that any questions any new languages are answering - it was asked by C++ (most of them) so C++ developers almost always know why any language feature was introduced (Java, Rust, C#)

Ответить
sebweb3r
sebweb3r - 15.08.2023 23:14

I'm not sure if it helped or scared me :-D

Ответить
TheJackiMonster
TheJackiMonster - 11.08.2023 14:37

So effectively Rust moves ownership of a value by default (likely to optimize end-recursive functions in a similar way as Haskell) and it makes all variables immutable by default. In comparison C/C++ copies a value by default and makes all variables mutable by default. So that explains why going from it to Rust creates such a headache until you get through this. Anyway good video to understand the concept.

Ответить
EyeInTheSky
EyeInTheSky - 03.08.2023 18:06

so basiclally Rust is just JS without closure?

Ответить
Javier Corado
Javier Corado - 27.07.2023 04:02

Plz, do the same for SQL and CSS

Ответить
Pa šutiraj deeečkooo!!!
Pa šutiraj deeečkooo!!! - 19.07.2023 14:02

great video! i still don't understand shit. a clear sign for me to stay away from the crab

Ответить
TheNullBox
TheNullBox - 06.07.2023 22:45

you awesome

Ответить
Ivan Kudinov
Ivan Kudinov - 06.07.2023 12:14

as somebody who came from a typescript-land the amount of 'whoah!' I did is huge xD

Ответить
Ricardo Fuhrmann
Ricardo Fuhrmann - 21.06.2023 08:15

Thanks, perfect explanation on a complex subject!

Ответить
etrock
etrock - 20.06.2023 19:36

Smashed that subscribe button and suddenly I'm seeing in 4K, understanding quantum physics, and speaking fluent Mandarin... I guess this is how Bradley Cooper felt in Limitless!

Ответить
Looming
Looming - 07.06.2023 00:37

I really love the rust book. Concepts like these are explained quite well.

Ответить
Reiland Eubank
Reiland Eubank - 10.05.2023 00:50

Very good video, I never quite understood what the borrow checker was doing until now. The one thing that confuses me, however, as predominant C++ user, is that I am used to "pass by value" meaning that you pass the value into a function and any changes made in the function won't be applied to the overarching object/variable, and pass by reference allows you to directly access the object and change it. Interesting!

Ответить
Henrique Marques
Henrique Marques - 09.05.2023 16:30

Ok, this is rocket science for me. This video made know that i know nothing about actual programming. Well, time to go back study.

Ответить
Pavlik Vaflezhuev
Pavlik Vaflezhuev - 05.05.2023 20:04

actually, Array.prototype.sort in js mutate array

Ответить
Julian Ari
Julian Ari - 05.05.2023 15:26

The only things I get from the video are nvim shortcuts and how cool it looks 😅

Ответить
Fikri Yudhistira
Fikri Yudhistira - 30.04.2023 13:22

I feel like this whole borrow checker mumbo jumbo came from the world of garbage collected language, where the main problem being everything is a reference/pointer.

Coming from the world of C++, I already have been doing a more advanced version of borrow checker. This is because I think C and C++ are the only two remaining of modern languages that uses value semantics rather than reference/pointer semantics.

In C++, the main operation is always copy. So the one thing you always asked yourself is, whether you are making a deep copy or a shallow copy. In general, you avoid shallow copy. If making a deep copy is non trivial, then you work with references.

In a single threaded program, a function parameter comes down to 3 things:
- type
- type const&
- type&
The first is making a copy (be careful with shallow and deep copy), the second is an immutable reference, the third is a mutable reference.

At this point, if you pass an immutable reference to a function, and you attempt to mutate it within the function, the compiler will stop you. Just like Rust.

I think the value of the borrow checker system that C++ can learn from is compiler should be able to save you when within in scope, rather than only between scope boundary.

That is, when you hold an immutable reference on the same scope as a mutable reference, Rust will stop you from mutating, but C++ won't.

However, this is where my point of the advance version of borrow checker comes in.

Do you remember the functional programming idiom? Ones that no one understood and hates?

The reason for the stigma towards functional programming is because the idiom requires you to work with value rather than reference/pointer.

In a world where most programming languages are predominantly reference/pointer semantics, it is easy to understand why it is so confusing and frustrating. Because when you jump to value semantics, the language itself makes the mechanism opaque. In most cases, it makes it difficult to reasons because there is no clear syntactic difference between value and reference/pointer, and the compiler/interpreter also cannot save you.

However, because C++ is designed with value semantics, adopting a functional idiom is comparatively rather painless.

I argue that functional idiom is an alternative solution to the problem that the borrow checker is attempting to solve.

I also argue that it is more advanced than the borrow checker exactly because of the value semantic. Here we have rvalue references.

Functional idiom is difficult to reason in a reference/pointer semantic world. Functional idiom is expensive in a value semantic world. C++ answer is the rvalue reference.

The reason people hates C++ is it has terrible defaults. No C++ programmer will argue with you on that. The reason for terrible defaults is because of backwards compatibility. And terrible defaults means the unassuming inexperienced programmer not only creates terrible code, they not only shoot themselves on the foot, they also blow everything and everyone around them up too. In fact, this is the exact reason why cpp2 by Herb Sutter is even a thing. A path towards changing the defaults.

To me, Rust is a bridge between reference/pointer semantic world and value semantic world. C++ on the other hand, have always been and will always be a bridge between the value world and the machine world.

Rust tackles ownership with reference/pointer and the borrow checker. C++ tackles ownerships with value and rvalue.

One last thing but as importantly that I am curious about the borrow checker, is whether it can solve the inherent problem of iterators that programmers always have to deal with, or whether it simply sidestep the problem altogether.

Ответить
Vinícius Hass
Vinícius Hass - 30.04.2023 04:50

I'm convinced

Ответить
godnonamesleft
godnonamesleft - 22.04.2023 22:55

Been working with javascript for the past few years but cut my teeth on C/C++. This concept of ownership is so fucking cool.

Ответить
klapaucius
klapaucius - 17.04.2023 09:52

Thank you!

Ответить
japrogramer
japrogramer - 15.04.2023 22:28

Elon Musk would be someone who doesn't understand the borrow checker and then release his own borrow checker

Ответить
Les Hommes du Pilly
Les Hommes du Pilly - 15.04.2023 20:04

To avoid memory leaks errors, just buy more ram lol

Ответить
TurtleKwitty
TurtleKwitty - 13.04.2023 00:49

On the one hand see ho rust is safe but on the other this really just reinforces what I already knew, c++ can be just as safe if you use const& accross the board like is the default in rust.

Ответить
Spaze @ Spotify
Spaze @ Spotify - 12.04.2023 12:12

"Rust, for the people"

Ответить
Oliver Weiss
Oliver Weiss - 11.04.2023 20:21

I am scared of lifetime annotations though 😭

Ответить
Kuno Dragon
Kuno Dragon - 06.04.2023 09:08

I was having trouble understanding this video. Thankfully, I subscribed, and now everything is clear!

Ответить
It's circular
It's circular - 01.04.2023 02:40

More videos like these and I'll convert to Rust once for all 😅

Ответить
Allemand Instable
Allemand Instable - 26.03.2023 02:05

the same but it's Python ?
would be incredible

Ответить
Igor Silvestre
Igor Silvestre - 21.03.2023 03:46

how does he get the compiler warnings and errors in-line like that? Can someone please show me the light?

Ответить