Rust Tutorial #3 - Variables, Constants and Shadowing

  Рет қаралды 116,417

Tech With Tim

Tech With Tim

Күн бұрын

Welcome back to another video! In this video I am going to be showing variables. Now, I know that sounds pretty basic and most of you already know variables, but they work very differently in Rust than in other languages. It's essential that we learn this fundamental before moving forward. Sit back, relax, and enjoy the video!
💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: programmingexpert.io/tim and use code "tim" for a discount!
📚 Playlist: • Rust Programming Tutorial
⭐️ Timestamps ⭐️
00:00 | Introduction
01:13 | Variables (let)
08:00 | Name Shadowing
12:11 | Constants
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
👕 Merchandise: teespring.com/stores/tech-wit...
📸 Instagram: / tech_with_tim
📱 Twitter: / techwithtimm
⭐ Discord: / discord
📝 LinkedIn: / tim-ruscica-82631b179
🌎 Website: techwithtim.net
📂 GitHub: github.com/techwithtim
🔊 Podcast: anchor.fm/tech-with-tim
🎬 My KZbin Gear: www.techwithtim.net/gear/
💵 One-Time Donations: www.paypal.com/donate?hosted_...
💰 Patreon: / techwithtim
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
⭐️ Tags ⭐️
-Tech With Tim
-Rust
-Rust Programming Language
-Rust Not The Game
-Learn Rust Programming
-Rust Tools
⭐️ Hashtags ⭐️
#TechWithTim #RustProgramming

Пікірлер: 133
@blobofblutack
@blobofblutack 2 жыл бұрын
I've also learned that you can put the name of the variable inside of the curly braces if you like, instead of outside of the double quotes. eg: let x = 10; println!("x is equal to {x}"); Figured I'd share since I personally prefer the readability of my code when I do it this way :)
@hachikoi-san3901
@hachikoi-san3901 2 жыл бұрын
yeah! I was about to write that, string literal is called, awfully similar to JS lol
@michalbotor
@michalbotor Жыл бұрын
you need to have a rather new version of rust to do it however. it might seem like a no issue, but at work it is not uncommon to find yourself using something older, because its tested and porting is dangerous. like java 8 or even java 7. rust is probably no different.
@michalbotor
@michalbotor Жыл бұрын
@June@97 readability for the devs
@iamwaver
@iamwaver Жыл бұрын
yes it is more readable and understandable compared to the tutorial.
@gabrielceolato2
@gabrielceolato2 Жыл бұрын
@June@97 Imagine having {}{},{}!!{}--{}++{}bla bla{}{}, apple, pear, grapes, bananas, etc and now you have to delete one specific in the middle of this mess, {apple}{pear}{grapes}{bananas} seems better
@eddwinnas
@eddwinnas 2 жыл бұрын
I think rust will be the next big language
@jasoncheng4250
@jasoncheng4250 Жыл бұрын
ye hopefully our effort pays off later
@thehollowknerd3858
@thehollowknerd3858 Жыл бұрын
I don't think it'll be one of those massive programming languages like Python or JavaScript. I think it will fall more in the range of c and c++ as a low level language. This is because it is a very low level language and more difficult to learn than most other languages pushing most beginners away from it.
@josephthomasehigie5809
@josephthomasehigie5809 Жыл бұрын
It’s only big when you build with it and what you build with it also matters
@user-sb5vt8iy5q
@user-sb5vt8iy5q 6 ай бұрын
​@@thehollowknerd3858that's like saying "meat will never be useful in a candy store", of course it won't because it's the wrong use case, you can't compare rust to Javascript, nor python.
@anthonyleong4238
@anthonyleong4238 Ай бұрын
Dang. You were right!
@Sakrosankt-Bierstube
@Sakrosankt-Bierstube Жыл бұрын
I am programming in Java since a lot of years now and the series was a nice intro to rust.. i started watching because i was bored and i actually think i'll take a deeper look into rust now.
@fabianbarraez5111
@fabianbarraez5111 2 жыл бұрын
I am using this playlist to start learning Rust once for all, everything goes great so far!
@robertmanigault625
@robertmanigault625 Жыл бұрын
Tim, I can’t express enough how much I enjoy your tutorials.
@ohwow2074
@ohwow2074 2 жыл бұрын
Been waiting for this for 3 days!!!!
@Chocalatil
@Chocalatil 2 жыл бұрын
same
@MeaTLoTioN
@MeaTLoTioN 11 ай бұрын
Just subscribed, fancied learning Rust (as a beginner) and your first 2 episodes were very well explained and I really appreciate how well you worded everything. Thanks for making these tutorials.
@ananthramvijayaraj4554
@ananthramvijayaraj4554 2 жыл бұрын
thanks for posting the whole series at once!
@AlokSingh-rj1gt
@AlokSingh-rj1gt 2 жыл бұрын
Name shadowing is common in statically typed languages like C++. e.g. if you do for(int i = 0; i
@spaghettiking653
@spaghettiking653 2 жыл бұрын
The fact that this is not the case in e.g. Python really caught me off guard. The C++ way makes way more sense in my opinion.
@GTLugo
@GTLugo Жыл бұрын
That's not name shadowing, that's static scoping. To elaborate, static scoping is what you described: you cannot use a variable outside of the lexical scoping. You can use the variable name as many times as you want as long as you are not inside the same static scope. Name shadowing is specifically where you are able to reuse the same variable name in the *same* static scope.
@AsyncEgg
@AsyncEgg 2 жыл бұрын
I've had so much trouble learning rust but you make it easy
@bobbobertson9325
@bobbobertson9325 Жыл бұрын
This is amazingly well explained. Thank you.
@Ch1llyWasNeverHere
@Ch1llyWasNeverHere 8 ай бұрын
this programming tutorial is great im a new programmer that started in c++ this was really understandable and really helpful and i understanded everything thank you for making this tutorial
@techbhanucomputer4389
@techbhanucomputer4389 2 жыл бұрын
tq soooo much tim, was waiting for this from 3 days.
@michaelguerrero7232
@michaelguerrero7232 Жыл бұрын
Tim, you make this super simple to understand even for a semi-noob like me... Thanx!!!
@RoyalAlchemist
@RoyalAlchemist Ай бұрын
Great videos brother. Thanks for your content king!
@sw-code6027
@sw-code6027 2 жыл бұрын
Yes we definitely want this course 🤧
@peql1521
@peql1521 4 ай бұрын
Awesome when I was starting Python, years back, your series helped a lot for fundamentals I know this will increase my understanding in Rust :)
@Chocalatil
@Chocalatil 2 жыл бұрын
I was so excited to see this video!
@timkiryachek9114
@timkiryachek9114 2 жыл бұрын
Thank you for sharing such useful knowledge.
@dreamhollow
@dreamhollow Жыл бұрын
Thanks for your tutorials. This is really cool.
@Good-and-Geeky
@Good-and-Geeky Жыл бұрын
Nice and clear explanations - easy to follow
@kifayatUllah712
@kifayatUllah712 Жыл бұрын
Dart's compiler messages are even more helpful and amazing.
@mortaldev4999
@mortaldev4999 2 жыл бұрын
Name shadowing seem like a really useful thing thankyou sensie
@brajagopalmukherjee1588
@brajagopalmukherjee1588 2 жыл бұрын
Don't stop this series ,and add project pls
@ndirangujane4074
@ndirangujane4074 7 ай бұрын
I am in the third lesson and I LOVE IT🔥🔥🔥🔥....Thanks Tim
@ricardorien
@ricardorien Жыл бұрын
It's bloody awesome you still using Sublime Text in 2023, it's a great IDE and fast as Sonic.
@JakeFace0
@JakeFace0 Жыл бұрын
5:00 Every day I'm reminded what a blessing python's f-strings are: print(f"x is: {x}")
@ltdbtc2944
@ltdbtc2944 Жыл бұрын
you can do that in rust without the f beforehand
@JakeFace0
@JakeFace0 Жыл бұрын
@@ltdbtc2944 why didn’t he do that then?
@itsMC411
@itsMC411 Жыл бұрын
@@JakeFace0 cause in this video or last he said he was only learning Rust for a few days
@neoness1268
@neoness1268 Жыл бұрын
The best Rust tutorial by far.
@Dr.Cosmar
@Dr.Cosmar 7 ай бұрын
Moving from python to Rust, and being an anarchist that I am. I experiment. Easier string formatting; instead of "println!("x is: { }", x)" you can just do "println!("x is: {x}")" For me personally, I always find this to look more readable, but I also have a deep hatred for commas. This is really just gonna depend on how you learned, I learned this way, and it feels nice that I can apply this in Rust, AND!! I don't even have to put an "f" at the beginning of the string, it just knows to format it. (python = print(f"x is: {x}" for context.)
@walter7812
@walter7812 8 ай бұрын
Thanks, it's an excellent tutorial
@alexandru6990
@alexandru6990 2 жыл бұрын
It would be really awesome if you could, at some point, make an "_alternatives_" video between Rust & for example Python's most used libraries: e.g. "The equivalent of python's requests lib is and it's used like this you can use it like this". Otherwise, great content. Keep it up :)
@ThomazMartinez
@ThomazMartinez 2 жыл бұрын
Is it overriding or storing in different RAM area as new ref?
@3.B.U.B.E
@3.B.U.B.E Жыл бұрын
you are a blessing ❤
@wlpSidewinder
@wlpSidewinder Жыл бұрын
I'm new to Rust but was going through their free book and you can also cancantonate string values to include the variable as in example println!("X is: {x}"); rather than comma notate it
@bossrazorback731
@bossrazorback731 Жыл бұрын
this is such a great help. thanks a bunch
@ifeanyinneji7704
@ifeanyinneji7704 5 ай бұрын
Thanks for this. Makes the code easier to read. Similar to f-string in python 😅
@wlpSidewinder
@wlpSidewinder 5 ай бұрын
@@ifeanyinneji7704 yea I was pleased by it too for the same reason 😂
@meisam3dfoxh467
@meisam3dfoxh467 Жыл бұрын
That is a good tutorial thank you
@curtisblake261
@curtisblake261 3 ай бұрын
When I first learned Fortran many years ago, you could say "5=123" and afterwards whenever you used the literal 5, its value would be 123.
@daniel7007
@daniel7007 Жыл бұрын
Awesome!
@someuser4166
@someuser4166 3 ай бұрын
regarding the variable re-declaration changing data types; would it be accurate to think of it like using &variable in c++? where instead of changing the datatype we're changing whats in that memory location? and the rust variable being more like a pointer than an actual variable?
@user-hu1ek6vs7y
@user-hu1ek6vs7y Жыл бұрын
Cool !!
@maxwellmuhanda7940
@maxwellmuhanda7940 Жыл бұрын
loved the cat at end of tutorial 2🤣🤣
@codeandtalk6
@codeandtalk6 2 жыл бұрын
Finally wait is over...🎉
@parthasarathimondal7190
@parthasarathimondal7190 Жыл бұрын
Hey you can use rust analyzer and code runner plugin for better productivity and intellisense for rust.
@albatroshd7945
@albatroshd7945 Ай бұрын
11:30 was a bit of weird to me to do something like this, but it definitely proofs the point of redefining something
@InMemoryOfNeo
@InMemoryOfNeo Жыл бұрын
you're the man
@subee128
@subee128 2 жыл бұрын
Thanks
@nerochu-luntai6205
@nerochu-luntai6205 14 күн бұрын
Feeling that Rust is the future and considering that C++ is my weak point, I believe Rust could be a solution to improve my skills.
@emexJT
@emexJT 2 жыл бұрын
any suggested secondary language with Rust?
@workflowinmind
@workflowinmind 2 жыл бұрын
very cool serie🤙
@occamsrazor1285
@occamsrazor1285 Жыл бұрын
A variables reference types (pointers) and constants data types? Basically; variables stored in the heap and constants on the stack?
@workflowinmind
@workflowinmind 2 жыл бұрын
I highly recommend Windows Terminal (and Powershell :) )
@ytfeelslikenorthkorea
@ytfeelslikenorthkorea 7 ай бұрын
I'm impressed with the compiler error handling. I wish python do that :)
@kvsbcsljv
@kvsbcsljv 7 ай бұрын
if you are new to programming, stay away from rust as much as you can. it is NOT gonna go smoothly (talking from experience)
@RobertLugg
@RobertLugg 5 ай бұрын
Are there global variables? If so are they global only to their file? Same questions of constants.
@MoJoeShoMo
@MoJoeShoMo 2 ай бұрын
You defined x to be an integer and printed it. Then a string and printed it. What would happen if you tried to reassign x on a 3rd attempt?
@mistymu8154
@mistymu8154 Жыл бұрын
I do like that Rust is immutable by default. However, coming from a language like Swift or Kotlin, I do find the distinction between const and let quite strange. In Swift and Kotlin you declare a variable with the var keyword, but the convention is to use constants by default, let in Swift and val in Kotlin, which both have type inference.
@berndf0
@berndf0 5 ай бұрын
The difference is that a "const" is a compile time expression while let defines a variable. An immutable variable can only be set once but it can be set to any run time expression, which may depend on other variables or an return values of a function call.
@levyroth
@levyroth 2 жыл бұрын
What would be Rust used for by a beginner?
@MarsMan2482
@MarsMan2482 Жыл бұрын
Rust is interesting to me as a embedded developer. It would be cool to use it for embedded in my work but c and c++ are so engrained into the industry that I don’t they they will leave any time soon
@shrimpoffthebarbie
@shrimpoffthebarbie Жыл бұрын
Are const arrays frozen?
@nihalnclt
@nihalnclt Жыл бұрын
@TheSavage1969
@TheSavage1969 2 жыл бұрын
It sure seems that it be a helluva lot easier to work with if variables were mutable by default. Any practically logical reason why they are immutable by default besides scoping?
@letsgocamping88
@letsgocamping88 2 жыл бұрын
My thought too. Why not just make them all mutable by default unless you declare them as a const?
@pup4301
@pup4301 2 жыл бұрын
Remember guys if you do got to edit the data then clone is your best friend.
@TravisGarnett
@TravisGarnett 2 жыл бұрын
@4:32 - 😎👍
@RedstoneHair
@RedstoneHair Жыл бұрын
the scope system is a lot like python, so I quickly understood it's workings
@antilogism
@antilogism 10 ай бұрын
And in C/C++ where it's referred to as a "compound" or "compound statement".
@tooru
@tooru 2 жыл бұрын
in println! what is the exclamation mark for?
@downfall6223
@downfall6223 2 жыл бұрын
I think that the thing is called "macros", basically it's like a function, but it has a few differences. He probably will talk about them in the future. If don't want to wait, I highly recommend you to check out "The Rust Programming Language" book, there is a section about this topic.
@tooru
@tooru 2 жыл бұрын
@@downfall6223 thank you
@Ranoth
@Ranoth Жыл бұрын
10:06 I guess a better way to show what is actually happening would be to make the first x mutable and not recreate it before the last print, just adding 1 and print the references of x on both scopes every time succeeding the print of x's value. You would then be left with Identical refs for the first and last prints.
@v037_
@v037_ 3 ай бұрын
7:55 no way rust is this way much flexible
@T8pr
@T8pr 26 күн бұрын
can anyone please explain what is u32?
@winwiths.g6155
@winwiths.g6155 9 күн бұрын
It is just a declaration of int 32, we also have int 64 which is called u64 in rust but both basically are the same, but they differ in compiling speed and precission u64 is much more precise but is a lil bit slow I would say (not really sure check it out), whereas u32 is much more faster to process for cpu's, due to which it is commonly used in rust while declaring integer types. Hope this helps!
@zqtmmadethat
@zqtmmadethat 4 ай бұрын
everytime i run cargo run it just makes a exe saying hello world instead of my actual code in src
@realmimak
@realmimak Жыл бұрын
redeclaration with the same name being a pattern is the most evil thing ive ever seen
@suretmeyenateistoc4252
@suretmeyenateistoc4252 2 жыл бұрын
why += not working bruh
@abdelhaksaouli8802
@abdelhaksaouli8802 Жыл бұрын
dude so Rust is C but with compiler that babysit you ,?
@G809
@G809 2 жыл бұрын
apparently the mutable variables have been changed (either that or my install of rust is different for some weird reason) and rust prompted me to remove the mut so yeah... edit: its because I had used let both times hah
@cristiandecu
@cristiandecu 3 ай бұрын
Rust: the language where all variables all constants by default and redeclaration is allowed.
@nurimep
@nurimep 11 ай бұрын
Tim, thanks But also your closing cmd is irriating me. alt+tab back, up on keyboard to get the cargo command back, then execute. Thanks for the series though!
@n_kliesow
@n_kliesow Жыл бұрын
So let in rust us the equivalent to the auto type in c++
@Gredddfe
@Gredddfe Жыл бұрын
It seems counterproductive to me to only enforce types on mutable variables. You're either going to encourage people to define everything as mutable, or you're going to allow people to undermine the static typing of the language by redefining variables. Either option seems bad to me.
@chaqua1559
@chaqua1559 Жыл бұрын
Nope. It's fine
@Gredddfe
@Gredddfe Жыл бұрын
@@chaqua1559 Would you care to elaborate? I'm here to learn.
@vask5500
@vask5500 2 жыл бұрын
You'll like rust analyser
@alienews0
@alienews0 3 ай бұрын
2:05 euh nope : it gonna EXplicitly assign a type to it, because u IMplicitly revealed it was an int by assigning the value 4 to it ;)
@MClapYourHands
@MClapYourHands 2 ай бұрын
Coming from C++, it feels so wrong to "redeclare" a variable like that.
@fustigate8933
@fustigate8933 2 жыл бұрын
First
@PRojekABC
@PRojekABC 7 ай бұрын
constant is not changeable in the entire scope - not entire program. It's allowed to redefine it in another scope. And you cannot use upper constant to calculate new constant in the lower scope. Allowed is: const VALUE_CONST: u32 = 20; println!("The VALUE_CONST is {}", VALUE_CONST); { const VALUE_CONST: u32 = 10; println!("The VALUE_CONST is {}", VALUE_CONST); } println!("The VALUE_CONST is {}", VALUE_CONST); Not allowed is const VALUE_CONST: u32 = 20; println!("The VALUE_CONST is {}", VALUE_CONST); { const VALUE_CONST: u32 = VALUE_CONST + 10; println!("The VALUE_CONST is {}", VALUE_CONST); }
@Roleplay78
@Roleplay78 Жыл бұрын
As someone that started with C many many years ago, all these new languages and styles and whatever, feels more as a hindrance than an actual tool to write better software. A Variable, by name, is variable. A compiler needs to compile the code, not telling me if I read or not a variable, especially if I am designing my algorithm.
@gnsf
@gnsf 2 жыл бұрын
Why does rust create new problems from scratch as language features? Why can't we have nice programming languages? Why they all do try very hard to be quirky?
@shrimpoffthebarbie
@shrimpoffthebarbie Жыл бұрын
Shadowing. Pretty much closure. This is borrowing from JS so much. Not a bad thing BTW.
@martijnwitteveen2007
@martijnwitteveen2007 Жыл бұрын
My eyes hurt
@_plsubscribe_
@_plsubscribe_ 2 жыл бұрын
Hi Tim, I wanna be a data freelancer as soon as possible. So please make a vid about it.
@gamemoves2415
@gamemoves2415 2 жыл бұрын
Lmao. Go and research on your own. You're not serious enough
@Itzcrystalroblox
@Itzcrystalroblox 11 ай бұрын
i dont think you need a :
@joshbishop
@joshbishop 2 жыл бұрын
Weird...my comment keeps getting deleted. Welp, cool vid anyway.
@jouiedomar1922
@jouiedomar1922 2 жыл бұрын
The background music is bad 😖, mute please 🤗.
@l3ertuz362
@l3ertuz362 11 ай бұрын
so far, not really a fan of rust syntax of declaring variables
@user-qr4jf4tv2x
@user-qr4jf4tv2x Жыл бұрын
double declaration sounds like a bad design the intent would be more clear if you just can't declare again its not like you can declare x2 x3 x4
@jasongracae1780
@jasongracae1780 2 жыл бұрын
Dude I just discovered that we have 'constant variables' in many programming languages... That doesn't make any sense. Or you may call it an oxymoron😎
@masudalimran92
@masudalimran92 3 ай бұрын
Cmon, be real. Every time you say 'If you are coming from another language', you are talking about JavaScript.
@hansaranzalez7860
@hansaranzalez7860 2 ай бұрын
This guy goes too fast
@nzqxt
@nzqxt 9 ай бұрын
what's the differents between a unmutable variable and a const variable ?
@yuvrajchandra2136
@yuvrajchandra2136 Ай бұрын
wait is it just me or any1 else is also facing this issue ? rustfmt isn't formatting the code on sublime/ vscode (tried on both)
Rust Tutorial #4 - Data Types
17:20
Tech With Tim
Рет қаралды 89 М.
Rust Tutorial #6 - Arithmetic and Type Casting
19:10
Tech With Tim
Рет қаралды 60 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 10 МЛН
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 10 МЛН
Rust Tutorial #9 - Memory Management, Heap & Stack
21:27
Tech With Tim
Рет қаралды 56 М.
Rust Tutorial #5 - Console Input
8:56
Tech With Tim
Рет қаралды 67 М.
Rust is not a faster horse
11:37
No Boilerplate
Рет қаралды 322 М.
The Ultimate Tier Programming Tier List | Prime Reacts
26:57
ThePrimeTime
Рет қаралды 366 М.
How To Practice Programming So You Actually Get Good
15:46
Tech With Tim
Рет қаралды 104 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 295 М.
Python Lambda Functions Explained
8:07
Tech With Tim
Рет қаралды 27 М.
Python Logging - Tutorial
15:02
Tech With Tim
Рет қаралды 154 М.
Rust makes you feel like a GENIUS
10:48
No Boilerplate
Рет қаралды 403 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 10 МЛН