Rust String vs str slices

Поделиться
HTML-код
  • Опубликовано: 15 дек 2024

Комментарии • 72

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

  • @TruePakistani-od3uf
    @TruePakistani-od3uf 4 месяца назад +1

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

  • @ronaldo5276
    @ronaldo5276 2 года назад +2

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

  • @dougmilford7814
    @dougmilford7814  5 лет назад +5

    Thanks for clicking 'Like' and Subscribing!

  • @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 года назад +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.

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

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

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

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

  • @wulymammoth
    @wulymammoth 4 года назад +4

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

  • @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 10 месяцев назад +2

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

  • @amoshnin
    @amoshnin 3 года назад +2

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

  • @irlshrek
    @irlshrek 2 года назад +1

    im so happy I found this channel!

  • @prueba875
    @prueba875 Год назад +1

    Wonderful video! Thanks!

  • @yurikolovsky
    @yurikolovsky 3 года назад +1

    Really good and eye opening

  • @mohamedmagdy428
    @mohamedmagdy428 3 года назад +1

    Your explanation is very good, thank you

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

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

  • @ManasTunga
    @ManasTunga 3 года назад +1

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

  • @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: ruclips.net/video/lQ7XF-6HYGc/видео.html

    • @TheArstman
      @TheArstman 4 года назад +1

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

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

  • @bangonkali
    @bangonkali 4 года назад

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

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

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

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

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

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

  • @janrusthaug6254
    @janrusthaug6254 5 лет назад +1

    Thanks a lot!

  • @吴彦祖-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 :)

  • @edgeeffect
    @edgeeffect 2 года назад +1

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

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

  • @muhammadshohidurrahman6887
    @muhammadshohidurrahman6887 4 года назад +1

    wunderbar

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