Latest / CyberCode Academy / Course 19 - Ultimate Rust Crash Course | Episode 2: Rust Core Foundations: Data Types, Control Flow, and String Handling
Transcript
- 0:00Speaker 1: Okay, let's unpack this. We've all gotten past that first
- 0:03Speaker 1: Hello World program, right, But then comes the real question,
- 0:08Speaker 1: how does a program actually hold on to information? And
- 0:12Speaker 1: you know, how do we control what it does? Step
- 0:14Speaker 1: by step? Today we're diving deep into the fundamental atoms
- 0:18Speaker 1: of programming, the data types exactly, the foundational types and
- 0:22Speaker 1: the control flow that make every piece of software work.
- 0:25Speaker 1: Our mission is to take things that feel well, overwhelmingly complex, things.
- 0:30Speaker 2: Like floating point precision or the absolute chaos of Unicode yeah.
- 0:33Speaker 1: And make it all clear, actionable knowledge. We want to
- 0:36Speaker 1: understand why these design choices were made in the first place.
- 0:39Speaker 2: What's fascinating here is that every single one of these
- 0:41Speaker 2: decisions is a trade off, usually for performance, and understanding
- 0:45Speaker 2: them isn't just you know, academic. It directly impacts your
- 0:48Speaker 2: code's memory, use, its speed, and critically how reliably it
- 0:52Speaker 2: handles real world.
- 0:53Speaker 1: Data, especially international data.
- 0:55Speaker 2: Absolutely, we're looking at the choice as a systems language
- 0:57Speaker 2: makes to get both maximum speed and uncompromising safety.
- 1:01Speaker 1: So let's start at the very bottom the foundation scaler types, the.
- 1:05Speaker 2: Four core building blocks, integers, floats, boollions, and characters.
- 1:10Speaker 1: Okay, integals, This is where it all starts right.
- 1:12Speaker 2: You have your signed integers, which have an I prefix
- 1:15Speaker 2: like I thirty two. They could be positive or negative.
- 1:18Speaker 1: And then the unsigned ones prefixed with.
- 1:20Speaker 2: You, like U eight, which are only zero and positive,
- 1:23Speaker 2: and that number the bit size is totally consistent. It
- 1:27Speaker 2: doesn't matter what machine you're compiling for.
- 1:29Speaker 1: That consistency is huge. But you get these two special
- 1:33Speaker 1: types that break the rule U size and I size.
- 1:36Speaker 1: Why do their sizes have to match the platform?
- 1:39Speaker 2: Well, think of them as the language's own memory navigators.
- 1:42Speaker 2: Their size has to match the architecture thirty two or
- 1:45Speaker 2: sixty four bit, so they can reliably represent any memory
- 1:48Speaker 2: address your program could possibly need to access.
- 1:50Speaker 1: So you'd use them for something like indexing a massive
- 1:53Speaker 1: array exactly.
- 1:54Speaker 2: They guarantee you can address every last bite.
- 1:56Speaker 1: But even with most systems being sixty four bit now,
- 1:59Speaker 1: the def fault integer is still I thirty two. That
- 2:02Speaker 1: says a lot about performance, doesn't it.
- 2:04Speaker 2: It really does. The default is I thirty two because
- 2:07Speaker 2: a lot of the time the processor's fastest path for
- 2:09Speaker 2: math is still optimized for thirty two bit chunks. Speed
- 2:13Speaker 2: just wins out.
- 2:13Speaker 1: And speaking of numbers, I love the little readability features
- 2:16Speaker 1: we've got hex octal binary. But the underscore is the
- 2:20Speaker 1: real game changer. Oh absolutely, being able to write one
- 2:23Speaker 1: hundred thousand dollars and have the compiler just ignore the underscores.
- 2:27Speaker 1: It makes reading huge numbers so much easier.
- 2:30Speaker 2: And a quick technical note on you eight you can
- 2:33Speaker 2: basically use it interchangeably with the word bite. It's just
- 2:36Speaker 2: a single eight bit chunk of data.
- 2:38Speaker 1: Okay, so what about floating point types?
- 2:40Speaker 2: Much simpler. Really, You've just got F thirty two and
- 2:43Speaker 2: S sixty four.
- 2:43Speaker 1: And the default is F sixty four. The bigger one.
- 2:46Speaker 2: Why is that precision? You get vastly more precision with
- 2:49Speaker 2: S sixty four and it follows the IEE seven to
- 2:52Speaker 2: fifty four standard. But this is a classic trade off.
- 2:56Speaker 1: Ah, I see where this is going.
- 2:57Speaker 2: If you're not on sixty four bit hardware, using at
- 3:00Speaker 2: sixty four can be a lot slower, So you really
- 3:01Speaker 2: have to be aware of your target architecture.
- 3:03Speaker 1: And there's a little format rule too, right.
- 3:05Speaker 2: Yeah, simple one afloat literal has to have a digit
- 3:08Speaker 2: before the dot. Yeah, so point one is fine, but
- 3:10Speaker 2: point one won't.
- 3:11Speaker 1: Work, Okay, Moving on to the logic Boollians, volliums.
- 3:15Speaker 2: Simple enough, true or false, always lowercase.
- 3:18Speaker 1: And the key thing here is that they are completely
- 3:20Speaker 1: separate from integers. You can't just use a one for true, no.
- 3:24Speaker 2: Type coercion at all. If you want to do math
- 3:27Speaker 2: with a boolean, you have to explicitly cast it. It's
- 3:30Speaker 2: a safety feature.
- 3:31Speaker 1: And finally, the last scaler the character or char.
- 3:35Speaker 2: Right, and this one is uh, it's kind of misnamed,
- 3:39Speaker 2: ah so because it doesn't represent a byte, it's not
- 3:41Speaker 2: even what you might see on the screen. A char
- 3:44Speaker 2: is a single Unicode scaler value.
- 3:46Speaker 1: And this is that paradox we talked about. A char
- 3:48Speaker 1: is always four bytes long.
- 3:50Speaker 2: Always internally, it's like UCS four or UT thirty two.
- 3:54Speaker 2: This guarantees that looking one up is a fast, constant
- 3:57Speaker 2: time operation.
- 3:58Speaker 1: But all our text files, our source code, everything is in.
- 4:01Speaker 2: UTF exactly, so in practice, the chart type is mostly
- 4:05Speaker 2: for you know, single characters you define in your code,
- 4:08Speaker 2: not for walking through a string of text. The fixed
- 4:10Speaker 2: size structure just doesn't align with how modern text is
- 4:13Speaker 2: actually encoded.
- 4:14Speaker 1: Okay, here's where it gets really interesting when we start
- 4:16Speaker 1: combining these atoms compound types.
- 4:18Speaker 2: Let's start with tupples, right, the heterogeneous collections.
- 4:21Speaker 1: Meaning they can hold different types of values all together.
- 4:24Speaker 2: Yep. You just wrap them in parentheses, an integer, afloat
- 4:28Speaker 2: whatever you need. It's a quick way to group related data, and.
- 4:31Speaker 1: Getting the data out seems pretty clean. You can use
- 4:34Speaker 1: dot syntax like chords tip oh, chords dot.
- 4:37Speaker 2: One, which is good because it emphasizes that the fields
- 4:39Speaker 2: can be different types.
- 4:40Speaker 1: Or you can just structure them, which feels more idiomatic. Oh.
- 4:44Speaker 2: Absolutely. Using a pattern like let xy chords to pull
- 4:47Speaker 2: everything out into variables at once is much cleaner.
- 4:50Speaker 1: But there's a weird limit tooples kind of break after
- 4:54Speaker 1: twelve items.
- 4:55Speaker 2: Yeah, they lose most of their functionality. It's not an accident, though.
- 4:59Speaker 2: It's a compiler train off to save on compile times exactly.
- 5:02Speaker 2: The compiler automatically implements helpful features for tuples, but it
- 5:06Speaker 2: stops at twelve to keep things reasonable. It's also a
- 5:09Speaker 2: signal to you, the.
- 5:10Speaker 1: Developer, well kind of signal.
- 5:12Speaker 2: If you need thirteen different unrelated fields, you probably shouldn't
- 5:15Speaker 2: be using a tuple. You should be defining a proper
- 5:18Speaker 2: named struct It's a gentle nudge towards better code.
- 5:22Speaker 1: Organization that makes a lot of sense. So from tuples
- 5:25Speaker 1: to arrays.
- 5:27Speaker 2: Right, And the key difference is that arrays are homogeneous.
- 5:30Speaker 1: All the items have to be the exact same type.
- 5:33Speaker 2: Correct. You define them with square brackets, and you can
- 5:35Speaker 2: even initialize one with a value an account like zero
- 5:38Speaker 2: point zero.
- 5:39Speaker 1: Five, and the type annotation always includes that count like
- 5:41Speaker 1: f thirty two to two.
- 5:42Speaker 2: Yes, the size is part of its type.
- 5:44Speaker 1: But just like tipples, they have this this other seemingly
- 5:48Speaker 1: arbitrary limit. They're fixed size, they live on the stack,
- 5:53Speaker 1: and they lose utility after thirty two elements.
- 5:56Speaker 2: It's the same exact problem. The compiler stops generating those
- 5:59Speaker 2: con default features for a raise larger than thirty two.
- 6:03Speaker 1: So it's another signal.
- 6:04Speaker 2: It's the language telling you, hey, if you need thirty
- 6:06Speaker 2: three items, you should probably be using a vector or
- 6:09Speaker 2: a slice. Those are designed for dynamic, larger collections on
- 6:13Speaker 2: the heap. It's basically a performance and safety guardrail.
- 6:15Speaker 1: All right, So we've stored our data and now we
- 6:17Speaker 1: need to control the logic. Let's talk control flow.
- 6:20Speaker 2: This is where a language can make some really interesting
- 6:22Speaker 2: choices to head off common bugs before they even happen.
- 6:26Speaker 1: Like with if expressions, no parentheses needed around the condition,
- 6:29Speaker 1: which is nice and clean.
- 6:31Speaker 2: But that condition must evaluate to a bool, not an integer,
- 6:35Speaker 2: not anything else, just true or false.
- 6:38Speaker 1: That rigidity is a security feature, isn't it It is.
- 6:41Speaker 2: It prevents all those ambiguous conditions you see in other
- 6:44Speaker 2: languages where a number could mean true. Here it has
- 6:47Speaker 2: to be explicit.
- 6:48Speaker 1: And the big one. If is an expression it can
- 6:51Speaker 1: return of value, it can, and.
- 6:53Speaker 2: If you want to use that value. There are a
- 6:55Speaker 2: few key rules. First, braces are always mandatory, which.
- 6:59Speaker 1: Kills that old C style bug where you forget braces
- 7:02Speaker 1: and only the first line is conditional exactly.
- 7:04Speaker 2: Second, all the blocks, the IF block, the LS block,
- 7:07Speaker 2: they all have to return the same type.
- 7:09Speaker 1: Okay, wait, you mentioned dropping the semicolon inside the block
- 7:13Speaker 1: if it's returning a value, Why is that one piece
- 7:15Speaker 1: of punctuation so important.
- 7:17Speaker 2: Think of a semicolon as ending a statement it says,
- 7:19Speaker 2: do this, then discard the result. If you leave it
- 7:22Speaker 2: off at the very end of a block, it's no
- 7:24Speaker 2: longer a statement. It becomes an open value, a tail.
- 7:27Speaker 1: Expression, and that's the value that gets returned.
- 7:30Speaker 2: That's the value. Because of this, you just don't need
- 7:32Speaker 2: confusing nested ternary operators anymore.
- 7:35Speaker 1: What about repetition loops.
- 7:37Speaker 2: You have the unconditional loop and the fact that it
- 7:39Speaker 2: is unconditional. Let's the compiler make some really cool optimizations.
- 7:44Speaker 2: You just have to use brake to get out.
- 7:45Speaker 1: And for nested loops you can use labels.
- 7:48Speaker 2: Yeah, you can put a tick in a name like
- 7:50Speaker 2: cleanupt loop on an outer loop, and then from deep
- 7:53Speaker 2: inside you can say break cleanup. It makes complex control
- 7:56Speaker 2: flow so much clearer.
- 7:58Speaker 1: The conditional loops, while and four are just built on
- 8:01Speaker 1: top of that.
- 8:01Speaker 2: Then pretty much a wild loop just runs until its
- 8:04Speaker 2: condition is false. It's really just syntactic sugar. For a
- 8:08Speaker 2: loop with an if dot condition.
- 8:09Speaker 1: Break at the top and no do wile loop.
- 8:11Speaker 2: Not built in, but you can make one easily. Just
- 8:14Speaker 2: put the brake condition at the end of an unconditional loop.
- 8:16Speaker 1: Body and four loops.
- 8:18Speaker 2: Four loops are for anything that's iterable. You see them
- 8:21Speaker 2: a lot with ranges or using the dot uter method
- 8:24Speaker 2: on a collection. This is also where functional programming fans
- 8:27Speaker 2: get happy, because you can chain methods like map and
- 8:30Speaker 2: filter and they're all lazily evaluated.
- 8:33Speaker 1: Let's talk about those ranges. Start dot end doesn't include
- 8:36Speaker 1: the end number.
- 8:37Speaker 2: Right, it's exclusive if you want to include it, you
- 8:39Speaker 2: need the dot dot equals syntax dot start end.
- 8:44Speaker 1: Okay, Now for the really tough one strings.
- 8:47Speaker 2: The part of programming where things always get complicated.
- 8:49Speaker 1: It's not the logic, it's the data, and text is
- 8:52Speaker 1: just tricky.
- 8:53Speaker 2: You're mainly dealing with two string types. First is the
- 8:56Speaker 2: string slice answer SCR. What's borrowed, immutable, just a pointer
- 9:01Speaker 2: and a length.
- 9:02Speaker 1: All the string literals in your code are slices, correct.
- 9:04Speaker 2: Then you have the capital S string that one's owned,
- 9:07Speaker 2: mutable and has a pointer, a.
- 9:09Speaker 1: Length, and a capacity, and the one guarantee they both share.
- 9:11Speaker 2: Both are guaranteed to be valid UTF eight.
- 9:14Speaker 1: So if we connect this back to what we said
- 9:15Speaker 1: about char being complex, right, why can't you just index
- 9:19Speaker 1: a string? Why can't I write miss string three?
- 9:21Speaker 2: This is the heart of it, the byte scaler graphene trilemma.
- 9:25Speaker 2: It all comes down to Unicode's complexity versus the performance
- 9:29Speaker 2: guarantees of indexing.
- 9:30Speaker 1: Okay, break that down first.
- 9:31Speaker 2: UTF eight is a variable with encoding. A single Unicode
- 9:35Speaker 2: scaler value can take one, two, three, or four bytes,
- 9:38Speaker 2: So by position doesn't map to character position.
- 9:41Speaker 1: And that's before we even get to what a human
- 9:42Speaker 1: thinks of as a.
- 9:43Speaker 2: Character exactly, because those scaler values can combine to form
- 9:48Speaker 2: a single graphene, like.
- 9:49Speaker 1: A letter with an accent mark or an emoji made
- 9:52Speaker 1: of multiple parts.
- 9:53Speaker 2: Yes, that's what a person considers one character. So indexing
- 9:58Speaker 2: into bytes is fast, but it might give you half
- 10:00Speaker 2: a character. Indexing into graphemes is what you want, but
- 10:04Speaker 2: doing that would be a slow, variable time operation. It
- 10:09Speaker 2: would violate the constant time guarantee of a ray style indexing.
- 10:12Speaker 1: So the language just forbids it. It forces you to
- 10:15Speaker 1: be explicit about what you want. Safety over convenience.
- 10:18Speaker 2: Precisely, to get data out safely, you use iterators. Dot
- 10:22Speaker 2: bytes give you the raw UTF eight bytes fast, but
- 10:25Speaker 2: maybe not meaningful.
- 10:26Speaker 1: It gives you the Unicode scalers, right, and.
- 10:29Speaker 2: If you really need the human readable graphemes, you have
- 10:31Speaker 2: to bring in an external library like Unicode disegmentation.
- 10:34Speaker 1: And as a replacement for indexing, iterators have that dot
- 10:37Speaker 1: end method yep.
- 10:39Speaker 2: It lets you get the nth element, but it does
- 10:41Speaker 2: it safely by actually walking the iterator.
- 10:44Speaker 1: Okay, let's wrap this up by seeing how these concepts
- 10:46Speaker 1: actually play together. For sure, let's look at a practical
- 10:49Speaker 1: example we looked at a pretty messy nested data structure.
- 10:53Speaker 1: Remember the mess variable a tuple with an array inside
- 10:57Speaker 1: which had another tupple inside.
- 10:58Speaker 2: Right, And the goal was to get to the boolean
- 11:00Speaker 2: true very deep inside it.
- 11:02Speaker 1: And the solution was that index chain mess hunt two
- 11:05Speaker 1: to one point zero.
- 11:06Speaker 2: It perfectly shows how you combine tuple indexing with array
- 11:10Speaker 2: indexing to navigate these complex dacked structures.
- 11:13Speaker 1: And on the control flow side, we summed integers from
- 11:15Speaker 1: seven to twenty three. The trick was doing it inclusively right,
- 11:19Speaker 1: so we had.
- 11:19Speaker 2: To use that dot dot equals range seven tween ty
- 11:22Speaker 2: three in the fore loop.
- 11:24Speaker 1: We also used a wile loop to see how many
- 11:26Speaker 1: times you could double in number starting from one before
- 11:28Speaker 1: it passed five hundred, and.
- 11:29Speaker 2: The answer was nine times.
- 11:31Speaker 1: Finally, let's talk about organizing code. As things get bigger,
- 11:34Speaker 1: you can't just keep everything in one file.
- 11:36Speaker 2: No, you move your core logic into a library file
- 11:39Speaker 2: lib dot rs.
- 11:40Speaker 1: And to make those functions available to your main executable,
- 11:43Speaker 1: you have to mark them as pub for public.
- 11:45Speaker 2: Then in your main file you use the project name
- 11:48Speaker 2: let's say it's ding machine, and the use keyword use
- 11:51Speaker 2: look gene dot function name brings that function into scope.
- 11:55Speaker 2: It's a clean way to separate logic from execution. So
- 11:58Speaker 2: final thoughts all back to the data types. I mean
- 12:01Speaker 2: really understanding that a character is four bytes and a
- 12:05Speaker 2: string is a variable with sequence of bytes. That knowledge
- 12:08Speaker 2: alone prevents so many subtle, hard to find bugs down
- 12:11Speaker 2: the line.
- 12:12Speaker 1: And the control flow being expression based.
- 12:14Speaker 2: It just encourages cleaner, more readable code with fewer mistakes.
- 12:18Speaker 2: The mandatory braces, the strict typing, it all ends up.
- 12:22Speaker 2: This raise is an important question for you to think about.
- 12:24Speaker 2: We talked about arrays being fixed size and limited to
- 12:27Speaker 2: thirty two elements, So how would you rewrite an exercise
- 12:30Speaker 2: that needs you to put coordinate data into a fixed
- 12:33Speaker 2: size array if that array suddenly needed to hold one
- 12:36Speaker 2: hundred coordinates, not just two.
- 12:38Speaker 1: What kind of data structure would you need then, and
- 12:40Speaker 1: how would you go about filling it? Something to mull
- 12:42Speaker 1: over as you continue building better, faster software. That's it
- 12:46Speaker 1: for this deep dive. We'll talk to you next time.