Rust Tutorial #3 - Variables, Constants and Shadowing

Поділитися
Вставка
  • Опубліковано 5 вер 2024

КОМЕНТАРІ • 136

  • @blobofblutack
    @blobofblutack 2 роки тому +108

    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 роки тому +9

      yeah! I was about to write that, string literal is called, awfully similar to JS lol

    • @michalbotor
      @michalbotor 2 роки тому +11

      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 2 роки тому +3

      @June@97 readability for the devs

    • @iamwaver
      @iamwaver 2 роки тому +6

      yes it is more readable and understandable compared to the tutorial.

    • @gabrielceolato2
      @gabrielceolato2 Рік тому +4

      @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 роки тому +67

    I think rust will be the next big language

    • @jasoncheng4250
      @jasoncheng4250 Рік тому +7

      ye hopefully our effort pays off later

    • @thehollowknerd3858
      @thehollowknerd3858 Рік тому +9

      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 7 місяців тому +3

      ​@@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 2 місяці тому

      Dang. You were right!

  • @defacube
    @defacube 15 днів тому +1

    4:32 wasnt sure if i would actually use this but right as i saw it tell you exactly what to do i changed my mind completely, im switching from c++ to rust

  • @AlokSingh-rj1gt
    @AlokSingh-rj1gt 2 роки тому +23

    Name shadowing is common in statically typed languages like C++. e.g. if you do
    for(int i = 0; i

    • @spaghettiking653
      @spaghettiking653 2 роки тому +8

      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 Рік тому +7

      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.

  • @Sakrosankt-Bierstube
    @Sakrosankt-Bierstube 2 роки тому +9

    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 роки тому +21

    I am using this playlist to start learning Rust once for all, everything goes great so far!

  • @ricardorien
    @ricardorien Рік тому +1

    It's bloody awesome you still using Sublime Text in 2023, it's a great IDE and fast as Sonic.

  • @Colaholiker
    @Colaholiker 8 днів тому

    As a seasoned C/C++ programmer who wants to take the first careful steps into Rust, I really appreciate your tutorial. In these two languages, I would call myself quite seasoned (after all, this is what has been paying my bills for the last 18 years), so a tutorial that doesn't introduce basic programming concepts but focuses on how things are done in a certain language is exactly what I am looking for. Therefore, big thanks!
    There's just one thing that I'd like to politely ask for - and maybe you are already doing it in more recent videos, I just stumbled across your channel, searching for a Rust tutorial. Please don't include background music while you are talking. While it is certainly something for intro and outro, when there is something going on "behind" your explanation, my ears always try to focus on that instead of what you are explaining. While I do lime cinematic videos having cool soundtracks, it doesn't add anything to a video of this type. Of course I don't expect you to upload the videos again without the music, it's just something you could possibly consider for the future if you haven't already.And I am sure I am not the only one who feels like this...

  • @robertmanigault625
    @robertmanigault625 Рік тому +12

    Tim, I can’t express enough how much I enjoy your tutorials.

  • @AsyncEgg
    @AsyncEgg 2 роки тому +5

    I've had so much trouble learning rust but you make it easy

  • @ohwow2074
    @ohwow2074 2 роки тому +9

    Been waiting for this for 3 days!!!!

  • @JakeFace0
    @JakeFace0 2 роки тому +2

    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

  • @wlpSidewinder
    @wlpSidewinder Рік тому +2

    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 Рік тому +2

      this is such a great help. thanks a bunch

    • @ifeanyinneji7704
      @ifeanyinneji7704 6 місяців тому

      Thanks for this. Makes the code easier to read. Similar to f-string in python 😅

    • @wlpSidewinder
      @wlpSidewinder 6 місяців тому

      @@ifeanyinneji7704 yea I was pleased by it too for the same reason 😂

  • @ananthramvijayaraj4554
    @ananthramvijayaraj4554 2 роки тому +5

    thanks for posting the whole series at once!

  • @MeaTLoTioN
    @MeaTLoTioN Рік тому +1

    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.

  • @ThomazMartinez
    @ThomazMartinez 2 роки тому +5

    Is it overriding or storing in different RAM area as new ref?

  • @Ch1llyWasNeverHere
    @Ch1llyWasNeverHere 10 місяців тому

    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

  • @peql1521
    @peql1521 5 місяців тому

    Awesome when I was starting Python, years back, your series helped a lot for fundamentals I know this will increase my understanding in Rust :)

  • @Dr.Cosmar
    @Dr.Cosmar 8 місяців тому

    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.)

  • @albatroshd7945
    @albatroshd7945 3 місяці тому

    11:30 was a bit of weird to me to do something like this, but it definitely proofs the point of redefining something

  • @alexandru6990
    @alexandru6990 2 роки тому +13

    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 :)

  • @michaelguerrero7232
    @michaelguerrero7232 Рік тому

    Tim, you make this super simple to understand even for a semi-noob like me... Thanx!!!

  • @sw-code6027
    @sw-code6027 2 роки тому +3

    Yes we definitely want this course 🤧

  • @brajagopalmukherjee1588
    @brajagopalmukherjee1588 2 роки тому +5

    Don't stop this series ,and add project pls

  • @parthasarathimondal7190
    @parthasarathimondal7190 2 роки тому +1

    Hey you can use rust analyzer and code runner plugin for better productivity and intellisense for rust.

  • @curtisblake261
    @curtisblake261 4 місяці тому

    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.

  • @RoyalAlchemist
    @RoyalAlchemist 3 місяці тому

    Great videos brother. Thanks for your content king!

  • @kifayatUllah712
    @kifayatUllah712 2 роки тому

    Dart's compiler messages are even more helpful and amazing.

  • @neoness1268
    @neoness1268 Рік тому

    The best Rust tutorial by far.

  • @ndirangujane4074
    @ndirangujane4074 8 місяців тому

    I am in the third lesson and I LOVE IT🔥🔥🔥🔥....Thanks Tim

  • @bobbobertson9325
    @bobbobertson9325 Рік тому +1

    This is amazingly well explained. Thank you.

  • @mistymu8154
    @mistymu8154 Рік тому +2

    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 6 місяців тому

      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.

  • @nerochu-luntai6205
    @nerochu-luntai6205 Місяць тому

    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.

  • @maxwellmuhanda7940
    @maxwellmuhanda7940 Рік тому

    loved the cat at end of tutorial 2🤣🤣

  • @RedstoneHair
    @RedstoneHair Рік тому +1

    the scope system is a lot like python, so I quickly understood it's workings

    • @antilogism
      @antilogism 11 місяців тому

      And in C/C++ where it's referred to as a "compound" or "compound statement".

  • @mortaldev4999
    @mortaldev4999 2 роки тому

    Name shadowing seem like a really useful thing thankyou sensie

  • @techbhanucomputer4389
    @techbhanucomputer4389 2 роки тому

    tq soooo much tim, was waiting for this from 3 days.

  • @Good-and-Geeky
    @Good-and-Geeky Рік тому

    Nice and clear explanations - easy to follow

  • @nihalnclt
    @nihalnclt Рік тому +1

  • @someuser4166
    @someuser4166 4 місяці тому

    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?

  • @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.

  • @dreamhollow
    @dreamhollow Рік тому

    Thanks for your tutorials. This is really cool.

  • @workflowinmind
    @workflowinmind 2 роки тому +1

    I highly recommend Windows Terminal (and Powershell :) )

  • @walter7812
    @walter7812 9 місяців тому

    Thanks, it's an excellent tutorial

  • @Gredddfe
    @Gredddfe Рік тому +3

    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 Рік тому +1

      Nope. It's fine

    • @Gredddfe
      @Gredddfe Рік тому +1

      @@chaqua1559 Would you care to elaborate? I'm here to learn.

  • @pup4301
    @pup4301 2 роки тому +2

    Remember guys if you do got to edit the data then clone is your best friend.

  • @TheSavage1969
    @TheSavage1969 2 роки тому +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?

  • @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

  • @MoJoeShoMo
    @MoJoeShoMo 3 місяці тому

    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?

  • @occamsrazor1285
    @occamsrazor1285 Рік тому

    A variables reference types (pointers) and constants data types? Basically; variables stored in the heap and constants on the stack?

  • @tanvirsalehin
    @tanvirsalehin 25 днів тому

    damn... warnings are cool

  • @v037_
    @v037_ 4 місяці тому

    7:55 no way rust is this way much flexible

  • @timkiryachek9114
    @timkiryachek9114 2 роки тому

    Thank you for sharing such useful knowledge.

  • @codeandtalk6
    @codeandtalk6 2 роки тому

    Finally wait is over...🎉

  • @Chocalatil
    @Chocalatil 2 роки тому

    I was so excited to see this video!

  • @user-hu1ek6vs7y
    @user-hu1ek6vs7y Рік тому

    Cool !!

  • @nurimep
    @nurimep Рік тому

    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!

  • @meisam3dfoxh467
    @meisam3dfoxh467 Рік тому

    That is a good tutorial
    thank you

  • @RobertLugg
    @RobertLugg 6 місяців тому

    Are there global variables? If so are they global only to their file? Same questions of constants.

  • @3.B.U.B.E
    @3.B.U.B.E 2 роки тому

    you are a blessing ❤

  • @PRojekABC
    @PRojekABC 8 місяців тому

    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);
    }

  • @subee128
    @subee128 2 роки тому

    Thanks

  • @levyroth
    @levyroth 2 роки тому

    What would be Rust used for by a beginner?

  • @InMemoryOfNeo
    @InMemoryOfNeo 2 роки тому

    you're the man

  • @TravisGarnett
    @TravisGarnett 2 роки тому

    @4:32 - 😎👍

  • @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

  • @n_kliesow
    @n_kliesow Рік тому

    So let in rust us the equivalent to the auto type in c++

  • @daniel7007
    @daniel7007 Рік тому

    Awesome!

  • @abdelhaksaouli8802
    @abdelhaksaouli8802 Рік тому

    dude so Rust is C but with compiler that babysit you ,?

  • @cristiandecu
    @cristiandecu 4 місяці тому

    Rust: the language where all variables all constants by default and redeclaration is allowed.

  • @zqtmmadethat
    @zqtmmadethat 6 місяців тому

    everytime i run cargo run it just makes a exe saying hello world instead of my actual code in src

  • @workflowinmind
    @workflowinmind 2 роки тому

    very cool serie🤙

  • @tooru
    @tooru 2 роки тому +1

    in println! what is the exclamation mark for?

    • @downfall6223
      @downfall6223 2 роки тому +3

      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

  • @emexJT
    @emexJT 2 роки тому

    any suggested secondary language with Rust?

  • @alienews0
    @alienews0 4 місяці тому

    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 3 місяці тому

    Coming from C++, it feels so wrong to "redeclare" a variable like that.

  • @realmimak
    @realmimak Рік тому

    redeclaration with the same name being a pattern is the most evil thing ive ever seen

  • @suretmeyenateistoc4252
    @suretmeyenateistoc4252 2 роки тому +1

    why += not working bruh

  • @gnsf
    @gnsf 2 роки тому +3

    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?

  • @Roleplay78
    @Roleplay78 Рік тому +2

    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.

  • @shrimpoffthebarbie
    @shrimpoffthebarbie Рік тому

    Are const arrays frozen?

  • @T8pr
    @T8pr 2 місяці тому

    can anyone please explain what is u32?

    • @winwiths.g6155
      @winwiths.g6155 Місяць тому

      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!

  • @vask5500
    @vask5500 2 роки тому

    You'll like rust analyser

  • @shrimpoffthebarbie
    @shrimpoffthebarbie Рік тому

    Shadowing. Pretty much closure. This is borrowing from JS so much. Not a bad thing BTW.

  • @l3ertuz362
    @l3ertuz362 Рік тому +1

    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

  • @masudalimran92
    @masudalimran92 4 місяці тому +1

    Cmon, be real. Every time you say 'If you are coming from another language', you are talking about JavaScript.

  • @_plsubscribe_
    @_plsubscribe_ 2 роки тому +2

    Hi Tim, I wanna be a data freelancer as soon as possible. So please make a vid about it.

    • @gamemoves2415
      @gamemoves2415 2 роки тому +5

      Lmao. Go and research on your own. You're not serious enough

  • @fustigate8933
    @fustigate8933 2 роки тому +1

    First

  • @Itzcrystalroblox
    @Itzcrystalroblox Рік тому

    i dont think you need a :

  • @martijnwitteveen2007
    @martijnwitteveen2007 Рік тому

    My eyes hurt

  • @joshbishop
    @joshbishop 2 роки тому

    Weird...my comment keeps getting deleted. Welp, cool vid anyway.

  • @jouiedomar1922
    @jouiedomar1922 2 роки тому +1

    The background music is bad 😖, mute please 🤗.

  • @jasongracae1780
    @jasongracae1780 2 роки тому +1

    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😎

  • @hansaranzalez7860
    @hansaranzalez7860 3 місяці тому

    This guy goes too fast

  • @CakeInvasion11
    @CakeInvasion11 Місяць тому

    fn, Let, println!
    : {}
    Rust is all over the place, this is so bad

  • @nzqxt
    @nzqxt 10 місяців тому

    what's the differents between a unmutable variable and a const variable ?

  • @yuvrajchandra2136
    @yuvrajchandra2136 3 місяці тому

    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)