Rust Tutorial #5 - Console Input

  Рет қаралды 72,678

Tech With Tim

Tech With Tim

Күн бұрын

Пікірлер: 50
@raulwolters2380
@raulwolters2380 2 жыл бұрын
4:19 Ok I know this might be confusing, but when you transfer ownership of a variable it is usually NOT copied! Only a select few types that implement the Copy trait are allowed to be implicitly copied like that. Ownership of a variable really only has to do with who is responsible for managing the memory. Giving ownership of a piece of data to someone else is essentially equivalent to telling the compiler you will never access that data again within the same scope. In your example, you supposed transferring ownership of a heap-allocated String to a function. Heap-allocated objects are never directly accessible to a function, since they don't live on the stack (they live on the heap), so ownership of a heap-allocated variable is really just ownership of a pointer to a place in memory. Internally, passing a "String" or a "&String" is the exact same thing: you're just passing a pointer to a place on the heap. I hope that it is now clear that passing a String vs a &String is _not the same thing_ as pass-by-reference vs pass-by-value. So what does it actually do? I already quickly mentioned that ownership has to do with a sort of contract with the compiler as to who is responsible for the piece of memory. With this contract the compiler can decide when to delete your stuff. *Transferring* ownership of a piece of data (so passing a String as "String" or "mut String") to a function tells the compiler that you do not want to access the data anymore outside of the function, so the compiler will destroy the object after the function has been run. *Borrowing* ownership of a piece of data (passing it as "&String" or "&mut String") in a function signature tells the compiler that you DO need the data after the function completed, so it can't go ahead and delete it for you yet. So why does read_line() take a borrowed String rather than an owned one? Well, if you want user input, you usually want to be able to look at it afterwards. Passing the owned String would've told the compiler it is ok to delete the string after it had been filled with user input. We don't want that, that's why you pass in a borrowed String.
@perc-ai
@perc-ai 2 жыл бұрын
How do u know so much
@AndrewDeVriesturtle17
@AndrewDeVriesturtle17 2 жыл бұрын
Well said!
@remi.scarlet.
@remi.scarlet. 2 жыл бұрын
Does that mean that after he passed "input" to the println! macro (and the console printed it out) input gets deleted? Since it wasn't passed by ref (&)?
@IanRiley915
@IanRiley915 2 жыл бұрын
@@remi.scarlet. println! is a macro and not a function so it can work differently. Macros are pre-processed before compilation so they can do essentially anything with their inputs. In this case, println actually rewrites its inputs to use borrowing.
@vs_gaming3013
@vs_gaming3013 Жыл бұрын
This was just a brilliant explanation! Thanks a lot, Raul! After Python Rust seems like a whole new world.
@NorteXGame
@NorteXGame 2 жыл бұрын
For anyone interested, the Result() in Rust can be somewhat compared to a Promise inside JS, just not asynchronous. But essentially it means it can be successful (and return Ok, or in JS .then) or unsuccessful (and return an Err, or in JS .catch).
@perc-ai
@perc-ai 2 жыл бұрын
Ty so much bro
@ojochegbe_
@ojochegbe_ Ай бұрын
Ty bro
@AbhinavKulshreshtha
@AbhinavKulshreshtha 2 жыл бұрын
4:20 Sentence you were looking for is "pass by reference" instead of "pass by value". Compared to C/C++, this feels more secure because we are explicitly marking the variable as mutable while on C there is no need for check regarding this as all variables are mutable by default, also error handling was not mandatorily required as it is here, but it was always a standard practice to implement error handling for inputs. Once again, Rust Tooling is awesome. If I compare traditional languages like C/Java/C#, to modern languages like elm/kotlin/swift and now rust, I feel that 1st party tooling ecosystem plays a great role in the language's claim for security. Not to mention the lessons learned by analysing the history of traditional languages.
@nicholasbicholas
@nicholasbicholas 2 жыл бұрын
I'm no expert but just since I didn't see anybody else talk about this in the comments. stdin != Standard Output In, but rather just Standard Input. Standard output, input, and error (stdout, stdin, stderr) are three devices that often but don't necessarily send and receive data to/from a terminal. It's very common to redirect them to and from other files or devices. For a terminal-bound user, beyond simply using programs that interactively request user input over stdin, it's also very common (and unixy) to form pipelines of commands where each command in the chain 'pipes' or connects its standard output to the next command's standard input, for example something like 'ps -a | grep process_name'.
@frozenn00b
@frozenn00b 2 жыл бұрын
Came here to say the same thing... stdin = standard in/standard input... standard output in makes zero sense. Data goes in, or out.. not out then back in.
@dyspatch8574
@dyspatch8574 2 жыл бұрын
Great! Thanks for teaching Rust to beginners and making it more popular. 👌
@TravisGarnett
@TravisGarnett 2 жыл бұрын
Thank-you, Tim: I like the use of the expect(), for error handling! 😎👍
@kamogelooliphant1730
@kamogelooliphant1730 Жыл бұрын
❤ I went through 5 videos enjoying the tutorials of rust
@CornishMiner
@CornishMiner 2 жыл бұрын
Useful detail Tim, it's one of the things I like about your videos.
@KamyanYT
@KamyanYT 2 жыл бұрын
Great video! I always love your content. Thanks for the upload, such an inspiration!
@fireplank7520
@fireplank7520 2 жыл бұрын
Thanks! Awesome series so far. Looking forward to the rest when you upload more.
@voidemon490
@voidemon490 2 жыл бұрын
4:22 I'm very new to rust so please correct me if I'm wrong. In rust like any other high level languages such as c and c++, you don't actually copy value of an object in heap by default, rather it copies the address that points to that object. Also it's worth mentioning that in rust each variable is responsible for managing their space in heap, meaning compiler will add the "drop(address)" invocation as soon as the variable becomes out of scope. However by passing the variable "input" to "read_line" function you also pass the responsibility (a.k.a Ownership) of it. As a result the call to "drop" function occurs at the end of the lifetime of the parameter in "read_line" function, that is at the end of the function. Which means the value of input becomes unavailable after the call to read_line function. Sorry for the lengthy explanation, just trying to share my understanding of rust with other learners. Keep learning 💪
@voidemon490
@voidemon490 2 жыл бұрын
You might also be curious how "&input" and "input" is different in this case. Simply by passing the reference instead of the variable itself you are only borrowing the value of the variable, so rust compiler will know that "read_line" function is only interested in the value and not the ownership of the value.
@Hellbending
@Hellbending 11 ай бұрын
Did you just call c and c++ high level languages? High level from what, assembly? 🤣🤣 Bro the only thing high level here is you 🌿💨
@mani_mincraft
@mani_mincraft Жыл бұрын
Wow great video and explantion!
@curtisblake261
@curtisblake261 6 ай бұрын
Procedural versus declarative programming comes to mind. I knew someone who couldn't grasp "x=x+1" because it seemed like an impossibility to them. I'd call theirs a declarative mindset.
@mykolamorozov2099
@mykolamorozov2099 3 күн бұрын
From performance consideration, does it make sense to use input as a ref in print macro to avoid value copy? For instance: println!(&input)
@subee128
@subee128 2 жыл бұрын
Thanks
@hazmat86
@hazmat86 2 жыл бұрын
Love this series! Think I have learned more with Tim than with all other instructors combined!!!
@ankitchetri2968
@ankitchetri2968 2 жыл бұрын
Hmm thankyou
@Dr.Cosmar
@Dr.Cosmar 9 ай бұрын
saying @mut ~3:49 threw me off. Meant to say &("and")mut I can't wait until my brain is mush like this lolol I can feel it slowly happening already with that method for getting input. Python... "u_in = input()|" Me: "I need to graduate to a better language, I said, it will be worth it, I said."
@m_t_t_
@m_t_t_ Жыл бұрын
you have 3 file descriptors - stdin, stdout, stderr. not std out in
@zenglingyu
@zenglingyu 2 жыл бұрын
please make this tut complete..
@guitaek4100
@guitaek4100 Жыл бұрын
4:32 this is actually wrong. But I don't know either how to explain that without explaining ownership. Maybe "moving" is a better word here
@curtisblake261
@curtisblake261 6 ай бұрын
_bstr_t used to do the heavy lifting in C++.
@pravachanpatra4012
@pravachanpatra4012 2 жыл бұрын
Can you collab with Tiff in Tech
@cl.5974
@cl.5974 Жыл бұрын
What do you use for IDE?
@funworld8379
@funworld8379 6 ай бұрын
sublime text editor
@longlong10203
@longlong10203 Жыл бұрын
Rust sucks at getting input other than strings like int or array/vector
@jumpingjoy7689
@jumpingjoy7689 2 жыл бұрын
quick poll: how do you pronounce std? A: s t d, individually. or? B: stid I personally use B
@frozenn00b
@frozenn00b 2 жыл бұрын
s-t-d or standard... stdin = standard in, stdout = standard out, stderr = standard error, etc. So either s-t-d or standard.
@diegomartin6332
@diegomartin6332 2 жыл бұрын
Can you teach Ruby?
@md.shazidalhasan6726
@md.shazidalhasan6726 2 жыл бұрын
Unlike python, Rust seems very restricted language
@AlexanderHyll
@AlexanderHyll 2 жыл бұрын
You are right, it is more restrictive. The other side of the coin is that these restrictions also, compared to python, give you super powers. It is hundreds of times faster than python, it is safer (much lower risk of undefined behavior), no garbage collection and many times more memory efficient. You use python when being unrestricted is more important. You use rust when you need performance, safety, or have resource constraints. Rust is not as difficult or restrictive as it looks at first glance either. Thats why we use both at my firm. P.S. note that restrictive here means forcing (mostly) good habits, there’s no limitations to what you can make in rust.
@md.shazidalhasan6726
@md.shazidalhasan6726 2 жыл бұрын
@@AlexanderHyll I got you
@ohwow2074
@ohwow2074 2 жыл бұрын
I like how it is a strongly typed language. Just like Ada.
@jma42
@jma42 2 жыл бұрын
this is why it is so hard, you have to do mind-bending logic on lifecycles to work around on its restrictiveness
@AlexanderHyll
@AlexanderHyll 2 жыл бұрын
@@jma42 It's much more daunting when you start out, before realizing how basic the concept really is. Your data can be written once or read by many. And that's it (kinda sorta). All you need to do is spend a little more time when you set out to write your program so that your data structure design works in this way. The first couple times it feels like the compiler is your worst enemy when you get the bat to fight it after having written some code. After a few times though, when you get the bat it's more like a dear old sparring partner helping you get better. So essentially yes I do agree it is a barrier to entry, but it's not really that mind boggling after you grasp the core concept (a concept which in essence is fairly simple).
@rami_atallah
@rami_atallah 2 жыл бұрын
Rust is a harder version of c++ 😂
@ohwow2074
@ohwow2074 2 жыл бұрын
Nothing is harder than C++. As someone with an intermediate knowledge of C++, Rust code makes a lot of sense to me. This is the beginning though. Let's wait and see what other concepts Tim will teach us.
@jma42
@jma42 2 жыл бұрын
they should be somewhat similar in difficulty but damn its sure hard to program in a rust way, so many stuffs on it are outlier to most languages.
@paraxicgaming5743
@paraxicgaming5743 Жыл бұрын
@@ohwow2074 ANSI/ISO enforced C code begs to differ, also assembly language
Rust Tutorial #6 - Arithmetic and Type Casting
19:10
Tech With Tim
Рет қаралды 64 М.
"كان عليّ أكل بقايا الطعام قبل هذا اليوم 🥹"
00:40
Holly Wolly Bow Arabic
Рет қаралды 10 МЛН
Сюрприз для Златы на день рождения
00:10
Victoria Portfolio
Рет қаралды 2,2 МЛН
Flipping Robot vs Heavier And Heavier Objects
00:34
Mark Rober
Рет қаралды 59 МЛН
Я сделала самое маленькое в мире мороженое!
00:43
Кушать Хочу
Рет қаралды 4,6 МЛН
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 315 М.
you need to build a RUST desktop app!!
27:21
Travis Media
Рет қаралды 320 М.
Why i think C++ is better than rust
32:48
ThePrimeTime
Рет қаралды 316 М.
How to Learn Rust
10:36
No Boilerplate
Рет қаралды 573 М.
Where is Rust being used?
11:46
Let's Get Rusty
Рет қаралды 103 М.
Rust and RAII Memory Management - Computerphile
24:22
Computerphile
Рет қаралды 230 М.
Rust for TypeScript devs : Borrow Checker
8:49
ThePrimeagen
Рет қаралды 225 М.
How to Crack Software (Reverse Engineering)
16:16
Eric Parker
Рет қаралды 629 М.
Factorio teaches you software engineering, seriously.
21:27
Tony Zhu
Рет қаралды 1,7 МЛН
"كان عليّ أكل بقايا الطعام قبل هذا اليوم 🥹"
00:40
Holly Wolly Bow Arabic
Рет қаралды 10 МЛН