Rust String vs str slices

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ธ.ค. 2024

ความคิดเห็น • 72

  • @TruePakistani-od3uf
    @TruePakistani-od3uf 4 หลายเดือนก่อน +1

    This channel is too underrated on TH-cam but info available here is must have for beginners ( like me) who struggle with rust. Complex things explained easily.

  • @noname4422
    @noname4422 4 ปีที่แล้ว +49

    There are a few things I feel like could have been made clearer in this tutorial:
    1. On the slide String and &str, it says “characters (u8s)”. This gives the impression that the characters themselves are 8-bit, when really, when inside of a string, they can be 8-bit, 16-bit, 24-bit, or 32-bit depending on the character, and the u8s are just representing bytes of memory encoding the string, not individual characters.
    2. A very easy mistake to make is to try to take a substring based on byte indices that split a character down the middle. Doing this will panic and crash the program, which I think is an even more likely mistake than trying to index off the edge of a string.
    3. I wish you had also mentioned that the indexing using .chars() is not the same as the indexing using square brackets, and that indices between the two systems are not compatible, and that using an index of one type for the other can lead to crashes as per (2). The former counts full Unicode code points, whereas the latter counts bytes.

  • @ronaldo5276
    @ronaldo5276 2 ปีที่แล้ว +2

    Finally, someone who can really explain things clearly and to the point!
    Thank you!!

  • @lyingcat9022
    @lyingcat9022 4 ปีที่แล้ว +20

    Good vid :) just a couple of points here though. Stings are indeed groups of characters But they are stored in groups of UTF-8 characters. UTF-8 are themselves groups of 1, 2, 3 or 4 bytes(u8 data types) in length.
    So strings are stored in Vec but when the machine reads them it looks at the first byte(u8) and checks if the most significant bit is 0 or 1. If it finds say... 0110_1101 and sees a 0 in the leading bit it know the UTF-8 character is only 1 byte(u8) long and reads it as ‘m’. But if it finds 1010_1001 and sees a 1 it knows the UTF-8 character is at least 2 bytes long and needs to check the next one.
    This is how Rust can use UTF-8 to represent a string in any human language. And this is why you can’t index into a string like... some_str[2]. Sure it would give you the byte at index 2 and would work fine for the first 127 characters in UTF-8 that only need one byte; but it would give you garbage data for the other 1,111,937 characters that need 2 or more bytes.

  • @m.adeeldanish4350
    @m.adeeldanish4350 3 ปีที่แล้ว +1

    Can't thank you enough for deep-dive. This is the first language that make you learn inner workings of compiler beforehand.

  • @photoniccannon2117
    @photoniccannon2117 ปีที่แล้ว +1

    This is one of the best videos I have ever seen on this topic. You delve into things in a "this is how you do it, and then this is why it's weird" approach, which makes far more sense to me because I can then connect the theory that follows with the behavior that I just learned.
    Really appreciate your approach, you just earned a sub from me.

  • @evlogiy
    @evlogiy 11 หลายเดือนก่อน +2

    I'm starting to learn Rust and this is definitely deserve a subscription. Well explained. Thank you, sir. ❤

  • @abhishekshah11
    @abhishekshah11 4 ปีที่แล้ว +15

    I can't tell you how thankful I am for this. I spent the entire day today trying to figure out how strings work in rust and the book wasn't helping. Also this video was uploaded on my 25th birthday :P

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +6

      Yeah, this is a tricky topic in Rust. Wish it wasn't. I'm glad you found the video helpful!

  • @FaxNight
    @FaxNight 5 ปีที่แล้ว +8

    Best Rust video tutorials I've found so far!

    • @dougmilford7814
      @dougmilford7814  5 ปีที่แล้ว +4

      Wow, quite the compliment! Thanks :)

    • @FaxNight
      @FaxNight 5 ปีที่แล้ว +2

      ​@@dougmilford7814 You're welcome, and thank you for teaching me so good!

  • @irlshrek
    @irlshrek 2 ปีที่แล้ว +1

    im so happy I found this channel!

  • @wulymammoth
    @wulymammoth 4 ปีที่แล้ว +4

    Doug, you've made my Rust journey so much more easy. Thank you so much!

  • @lyingcat9022
    @lyingcat9022 4 ปีที่แล้ว +15

    The complexity of strings in Rust is why the book try’s to cover deeper topics first before diving into strings. Strings have been a complicated problem in computer science for decades.
    Might I suggest learning about these first...
    Rust ownership, borrowing and references.
    How and what is stored on the heap and stack(also what the heap and stack are)
    And some basics on the binary (base 2) number system.
    Also do a quick read on how the UTF-8 character encoding works.
    Some knowledge in these areas will make understanding strings waaaay easier :)

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +4

      Thanks for the suggestions, Nick! Yes, strings are much more complex than most languages let on, and they certainly make them feel easier than they truly are. I wrestled with order to do the tutorials for the very reasons you mentioned. I appreciate the feedback :) Maybe it will help others who are struggling with the topic.

    • @lyingcat9022
      @lyingcat9022 4 ปีที่แล้ว +1

      Doug Milford thank you for you vids :) I think they will help people for sure!

  • @lyingcat9022
    @lyingcat9022 4 ปีที่แล้ว +6

    Second small, possible slip of the tongue at 5:43 the & symbol is Not the syntax for “dereference” it is the syntax for a “reference”, the * symbol is for dereference. What the code here says to the Rust compiler is...
    Let this variable own a value of type &str(string slice) and let that value be an immutable reference to the entire String that this other variable owns.
    The compiler knows to give you a reference with a pointer to the address in the heaps memory where the Strings data(a Vec) is stored.

    • @JDalmasca
      @JDalmasca ปีที่แล้ว +2

      Glad someone else caught this too!

  • @amoshnin
    @amoshnin 3 ปีที่แล้ว +2

    You are of the best programming teachers! Thanks so much!

  • @mohamedmagdy428
    @mohamedmagdy428 3 ปีที่แล้ว +1

    Your explanation is very good, thank you

  • @dougmilford7814
    @dougmilford7814  5 ปีที่แล้ว +5

    Thanks for clicking 'Like' and Subscribing!

  • @Baron-digit
    @Baron-digit 4 ปีที่แล้ว +1

    Doug, once again this helps a lot to clarify some principles and ease the steep learning pain :-)
    Coming from Python to rust is hard, but as you mentioned, one starts to love the compiler to fix the code.

  • @prueba875
    @prueba875 ปีที่แล้ว +1

    Wonderful video! Thanks!

  • @TheArstman
    @TheArstman 4 ปีที่แล้ว +1

    Thank You! I am struggling to fight with rust compiler now and your video are the most helpful ever !!

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +1

      Yep, it's a learning curve :) If you're having trouble with the compiler, I suggest the "Ownership and Borrowing" video. Here's the link: th-cam.com/video/lQ7XF-6HYGc/w-d-xo.html

    • @TheArstman
      @TheArstman 4 ปีที่แล้ว +1

      @@dougmilford7814 Thanks, you are the best! hope to see more videos specially for generics~

  • @yurikolovsky
    @yurikolovsky 3 ปีที่แล้ว +1

    Really good and eye opening

  • @aj35lightning
    @aj35lightning 4 ปีที่แล้ว +1

    Thank you for making these approachable to someone new to programming. I have mostly python experience and have wanted to make the jump to a typed language, but most rust tutorials ive come across approach it from the point of view that you already know some c derivative. So it's always been a goal that i put off.
    until now. thank you

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +2

      Yeah, some Rust videos assume you already know what you're doing, which is ironic. I'm glad I hit the target :)

    • @peterisawesomeplease
      @peterisawesomeplease 4 ปีที่แล้ว +1

      Yea 90% of the people learning rust are trying to find a way out of c++ and c that essentially have no other direct competitors. Rust being statically typed(what you mean by typed, python is typed) is actually not the big jump from python. Going from python to a statically typed language like go, java, c#,... is a much smaller jump than going from python to Rust. The big jump with rust compared to python is you are going from a language with automatic memory(and more generally resource) management to one that enables manual management. The jump from python to rust is similar to the jump from python to c or c++. Statically typed languages are generally faster than dynamically typed languages like python but there is another order of performance gained by going to manual memory management.
      I think rust is starting to attract a lot of people who don't care about performance though. It has totally orthogonal features that make it really attractive. Choosing traints and composition over inheritance. The "match" functionality from Haskell. And several other things are so cool that people are willing to put up with the things rust does for performance even if they don't care about performance. I wish there was a rust "light" implementation that took all the nice parts of rust without caring about performance. So for example this version of rust would have only one String type that is heap allocated.

    • @aj35lightning
      @aj35lightning 4 ปีที่แล้ว +1

      @@peterisawesomeplease Yeah thats kind of what I'm getting at with coming to rust from python. I want to reap the benefits of a low level language, I've heard lots of great things. And if i'm going to sink a lot of time breaking my head learning this I want it to be rust because of its wasm tooling. Im not as interested in c/c++ rn. But most rust tutorials assume you already understand how to program in c/c++ so they skip lots of fundamentals that I have little reference for. So this tutorial series is very refreshing since it doesn't make those assumptions.

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +3

      @@peterisawesomeplease All good points and I agree completely. I, too, wish certain things were better in Rust (Strings and str are perfect examples). Whenever you take a new approach, you gain something, but also give something up. That took real courage for the Rust team to break the mold that has been around for several decades, and it was sorely needed. Most languages are just a copy of some previous language with only minor tweaks. Rust took the bull by the horns and really tackled the major thorns with software development. Granted, it did make it more difficult to comprehend.
      That said, Microsoft is working on a new language called Verona that has a foundation in Rust. They're thinking of rewriting their operating in it because c++ causes a VAST majority of the system's security issues due to poor memory management (something like 70+% of their issues). It's a huge amount of money they spend trying to address those memory gotchas that cause the security issues. They may just come up with something even better than Rust. Who knows. I'm really interested to see more about it. Hopefully they ease some of the development pain because Rust-like concepts are truly important.

  • @motbus3
    @motbus3 2 ปีที่แล้ว +1

    Hey
    Thank you. Great talk on this topic!
    Just a question, do you have any other videos on arrays and vectors? You mentioned it in this video but it seems to not be listed.
    Thank you

  • @ManasTunga
    @ManasTunga 3 ปีที่แล้ว +1

    One of best explained video thank you for giving your valuable time to make this

  • @dannyfoo5180
    @dannyfoo5180 5 ปีที่แล้ว +2

    Thank you for your video. Your tutorial on string has helped me understand this topic so so much better, kudos! Are you also planning to make project video or live coding session?

    • @dougmilford7814
      @dougmilford7814  5 ปีที่แล้ว +2

      Hey, I'm THRILLED you like the video :) To answer your question, I'm currently working on a video about doing 3D graphics in a browser. It will be more about getting up something from scratch instead of a topical tutorial. I'm hoping to have it out in a couple of weeks.

    • @dannyfoo5180
      @dannyfoo5180 5 ปีที่แล้ว +1

      @@dougmilford7814 you are legend.. will definitely check them out when its out

  • @bangonkali
    @bangonkali 4 ปีที่แล้ว

    Awesome content so far. Been following the Playlist 👍👍👍

  • @sanders927
    @sanders927 5 ปีที่แล้ว +1

    Thank you! This was really helpful!

  • @spguy7559
    @spguy7559 3 ปีที่แล้ว

    hi sir , would you keep teaching more Rust in the future ?
    you're best rust teacher upuntill now for rust . and my gratitude to you

  • @cerulity32k
    @cerulity32k 2 ปีที่แล้ว +1

    2:24 Rust’s char type is not identical to a u8, as a Rust char can handle any Unicode character, and even though regular characters use one byte, special characters like emojis can use up to 4 bytes to represent a character.

  • @daveturnbull7221
    @daveturnbull7221 4 ปีที่แล้ว +1

    I've got a headache...😒 As a self-taught 'dabbler' in javascript this is some seriously heavy lifting but I think it may be well worth the effort. Most of the other Rust videos consist of somebody going on at speed using terminology I have no idea about. Thanks you very much for the 'entry level' videos. now where did I put those painkillers...

  • @janrusthaug6254
    @janrusthaug6254 5 ปีที่แล้ว +1

    Thanks a lot!

  • @edgeeffect
    @edgeeffect 2 ปีที่แล้ว +1

    That was the big hurdle learning Ada.... in "safety first" languages, it seems, strings aint so easy.

  • @canardeur8390
    @canardeur8390 4 ปีที่แล้ว +1

    Thanks for this tutorial about String vs. Slices.
    It is true it is a pain in the ass to understand it but after your explanations, it makes sense.
    One question, when you write _let str_from_string: &str = &example_string;_ is there any good practice about naming conventions for references vs. variables?
    We do this in Javascript when we code _var myElement_ for Javascript objects or _var $myElement_ for jQuery objects to differentiate both types.

    • @MikeCOYS
      @MikeCOYS 4 ปีที่แล้ว

      I think you're getting mixed up here. There's no special naming convention for references. It's like asking if there is a naming convention for integers.

  • @muhammadshohidurrahman6887
    @muhammadshohidurrahman6887 4 ปีที่แล้ว +1

    wunderbar

  • @吴彦祖-r9h
    @吴彦祖-r9h 5 ปีที่แล้ว +2

    Thank you. Looking forward to more Rust courses!

    • @dougmilford7814
      @dougmilford7814  5 ปีที่แล้ว +6

      I'm glad you enjoy them! I'm working to publish a mini-series on Rust 3D Graphics in the Browser. Hopefully it'll be out in the next day or two. Should be fun :)

  • @arturczajkowski4255
    @arturczajkowski4255 4 ปีที่แล้ว +2

    I really hope that rust will live up to its expectations, meaning no data races, no memory corruption etc. For the moment I'm not "wowed" by it as it looks and feels awkward. But IF it delivers what it promises I can live with its awkwardness. Also, lack of debugger is simply a very weak point. Does anyone working on commercial product ever works without debugger? I mean, seriously, printing stuff is "so last decade" (or two or even three).

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +2

      There's debugger capability in VS Code for Rust... I just didn't use it during the video. For quick and dirty demonstrations, I often use the println in the videos just to quickly get feedback from the code. But you're right, not having a debugger would be unfeasible. I didn't mean to imply that you couldn't set breakpoints and such. No professional developer would be able to rely on just printing to the console.
      Below is an article that walks you through how to set up debugging in VS Code.
      www.forrestthewoods.com/blog/how-to-debug-rust-with-visual-studio-code/
      Regarding your "awkward" remark, it is awkward at first. You're developing in a way that is completely different than any other language currently out their, so there will be a period of time where things feel odd. Very common to feel that way at first.
      Let me know if you have issues with the debugger.
      Doug

    • @arturczajkowski4255
      @arturczajkowski4255 4 ปีที่แล้ว +2

      @@dougmilford7814 Thank you very much for your reply. I'll try to set up the debugger and will let you know! Best regards

    • @arturczajkowski4255
      @arturczajkowski4255 4 ปีที่แล้ว +2

      @@dougmilford7814 Hi there, managed to figure out and configure the debugger. Thank you very much.

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +2

      @@arturczajkowski4255 Yay! I love it when a plan comes together :)

  • @arturczajkowski4255
    @arturczajkowski4255 4 ปีที่แล้ว +1

    Great video as usual. TBH I am disappointed with Rust compiler that it cannot detect &some_example[0..200] as out of array bounds access. I mean, they (people from Rust) boast that their compiler can detect every possible dead lock/data race/memory corruption and they cannot detect this^^^. Weak.

    • @dougmilford7814
      @dougmilford7814  4 ปีที่แล้ว +2

      For most situations, you'd be passing in a String or str into a function, and there's no way for the compiler to know if it's in bounds or not. However, I'm wondering if there's a better way of dealing with strings altogether. Rust has done a fantastic job of breaking the mold and thinking differently about programming, but for strings it just feels... wrong. It feels like there's a much better solution just waiting to be invented.
      Microsoft is working on a new language called Verona that's based on Rust (in part). They may come up with something the fills in the gaps that Rust has trouble with.

    • @arturczajkowski4255
      @arturczajkowski4255 4 ปีที่แล้ว

      @@dougmilford7814 Yes, I mean seriously, strings in rust feel simply at best half baked and really, really weird. At least that's the first impression. Thank you for your replies. Really appreciate it.

    • @YanVidz
      @YanVidz 3 ปีที่แล้ว

      You should only ever index using square brackets when you are sure it isn't out of bounds. If you're unsure, there's the safe operation `.get()` that returns an Option instead. If you're really really sure, you can use the really unsafe `.get_unchecked()` method to skip any kind of checking, resulting in undefined behavior instead of crashing if it is out of bounds, but it is a tiny bit faster.

    • @arturczajkowski4255
      @arturczajkowski4255 3 ปีที่แล้ว

      @@YanVidz Surely this (your explanation about using or not square brackets) can be applied to number of languages, C++ amongst them? My point is that rust boast about being that much safer and yet, such a simple and obvious place is completely left unchanged when compared to C++. Disappointing...

    • @YanVidz
      @YanVidz 3 ปีที่แล้ว +2

      ​@@arturczajkowski4255 You misunderstand what safety Rust is talking about. Crashing the program and returning an error is considered safe. Not crashing the program and instead causing undefined behavior is unsafe. Avoiding UB is what Rust excels in compared to the other languages. Whether you wanna index or not, is always safe because if you failed to realize your own code then Rust will help you and crash the program instead of causing UB.
      Safety is remained in this matter.

  • @justiceessiel6123
    @justiceessiel6123 11 หลายเดือนก่อน +1

    if you learn golang rust is easier