Latest / CyberCode Academy / Course 19 - Ultimate Rust Crash Course | Episode 1: Rust Programming Foundations: From Cargo Tooling to Core Syntax and Modules
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. Today, we're plunging into
- 0:03Speaker 1: the foundational concepts of modern systems programming. We'll be looking
- 0:07Speaker 1: at a language that's really celebrated for its speed, but also,
- 0:11Speaker 1: and this is crucial, it's ironclad safety guarantees.
- 0:13Speaker 2: Exactly.
- 0:14Speaker 3: We're not just looking at the syntax here. We're really
- 0:17Speaker 3: examining the whole architecture of a successful project. It's about
- 0:20Speaker 3: a philosophy. You know, how a modern language can be
- 0:23Speaker 3: structured from the ground up to just prevent entire classes
- 0:27Speaker 3: of errors before you even run the code.
- 0:29Speaker 1: That's a huge shift for anyone coming from older languages.
- 0:32Speaker 3: Oh absolutely, It's a different world from crossing your fingers
- 0:36Speaker 3: and hoping things don't crash in production.
- 0:37Speaker 1: Okay, so let's unpack that starting with the tool that
- 0:40Speaker 1: makes it all happen, Cargo. Now, for people coming from
- 0:44Speaker 1: other ecosystems, they're used to stitching tools together, right, A
- 0:47Speaker 1: package manager here, a build tool.
- 0:49Speaker 3: There, a test runner, a documentation generator, all separate things.
- 0:53Speaker 1: Right, and Cargo is this all in one powerhouse that
- 0:56Speaker 1: just combines all of those.
- 0:58Speaker 3: It is such a huge quality of life improvement. For decades,
- 1:02Speaker 3: systems programmers were stuck with you know, these massive make
- 1:05Speaker 3: files or custom scripts.
- 1:07Speaker 1: A total mess.
- 1:08Speaker 3: Yeah, and Cargo just gives you that unified tool set
- 1:11Speaker 3: right out of the box. Think of it like the
- 1:13Speaker 3: best parts of NPM, Python's PIP and make all rolled
- 1:16Speaker 3: into one. It just simplifies everything.
- 1:19Speaker 1: So if you're starting out, the first practical step is
- 1:21Speaker 1: just a single command.
- 1:22Speaker 2: Yep, cargo new and then your project's name.
- 1:25Speaker 1: Let's say Cargo new Deep Dives project. Cargo instantly creates
- 1:28Speaker 1: the directory, it populates it, and you're ready to go.
- 1:31Speaker 3: And when you look inside you see that scaffolding the
- 1:33Speaker 3: config file cargo dot tom L, and then your source
- 1:36Speaker 3: code folder src with main dot rs inside, and.
- 1:40Speaker 1: Those file extensions are specific. The configuration is tom L,
- 1:43Speaker 1: Tom's obvious minimal language.
- 1:45Speaker 3: Which is really nice and human readable. And all your
- 1:48Speaker 3: code files end in topowires.
- 1:49Speaker 1: Let's open up that control center then cargo dot tom L.
- 1:52Speaker 1: This is more than just a config file. It feels
- 1:53Speaker 1: more like the project's manifesto.
- 1:55Speaker 2: It really is. It's the authoritative source.
- 1:57Speaker 1: So what are the key settings in there?
- 1:58Speaker 3: Well, the first is name. That's the official project name,
- 2:01Speaker 3: and it's totally independent of say, your directory or your
- 2:05Speaker 3: git repo name okay.
- 2:06Speaker 1: And then there's version. This one has some strict rules,
- 2:10Speaker 1: doesn't it has to use semantic versioning.
- 2:12Speaker 3: It does, and that's so important for the whole ecosystem.
- 2:15Speaker 1: Can you break that down? Severs one of those things
- 2:17Speaker 1: people hear about. But the three numbers have very specific.
- 2:20Speaker 3: Meanings, right, It's all about communicating change. It's major, minor,
- 2:24Speaker 3: dot patch. The last number patch is just for backward compatible.
- 2:28Speaker 2: Bug fixes, simple stuff, okay.
- 2:30Speaker 3: The middle number minor is for when you add new features,
- 2:33Speaker 3: but in a way that doesn't break anything for existing users.
- 2:37Speaker 1: And the first one major, that's the big one.
- 2:39Speaker 3: That's the big one. You only change that when you
- 2:41Speaker 3: make a break and change. It's a signal to everyone
- 2:44Speaker 3: that they might need to update their own code to
- 2:46Speaker 3: use your new version. It's how stability is managed.
- 2:49Speaker 1: That transparency is huge. You also see an addition line
- 2:52Speaker 1: like addition hinted twenty twenty one.
- 2:55Speaker 3: Yeah, that just tells the compiler which version of the
- 2:57Speaker 3: language standard to use. And then of course there's the
- 3:00Speaker 3: dependency section, which we'll get to later.
- 3:02Speaker 1: The amazing thing is how practical cargo is. Right away,
- 3:05Speaker 1: you have this structure and you can run the program
- 3:08Speaker 1: with just one command, cargo run, and it handles the
- 3:11Speaker 1: build in the execution all.
- 3:12Speaker 2: In one step. And here's the efficiency part.
- 3:15Speaker 1: I love.
- 3:16Speaker 3: If you run it again and nothing has changed in
- 3:19Speaker 3: the code.
- 3:19Speaker 1: Just it doesn't recompile.
- 3:21Speaker 3: Nope, it's smart enough to know it's already done the work.
- 3:24Speaker 3: It also keeps your project tidy. All the compiled files,
- 3:27Speaker 3: the binaries, they all go into one directory, Target, and that's.
- 3:31Speaker 1: A critical point for version control, right, Yeah, that target
- 3:34Speaker 1: directory should always always be in your dot git getingle always.
- 3:39Speaker 3: You never track compiled assets, only the source code.
- 3:42Speaker 1: And because this is system's programming, performance matters, so they
- 3:46Speaker 1: are different modes. By default, you get a debug build YEP.
- 3:49Speaker 3: With all the debug symbols in target debug. But if
- 3:52Speaker 3: you want speed, you have the release flag exactly Cargo
- 3:55Speaker 3: run release. And that's the key trade off. It takes
- 3:57Speaker 3: a little longer to compile because the compiler is doing
- 3:59Speaker 3: all these heavy optimizations.
- 4:01Speaker 1: But the final program, which ends up in target release
- 4:04Speaker 1: is way.
- 4:05Speaker 2: Faster, significantly faster.
- 4:07Speaker 3: And if you just want to check if your code
- 4:09Speaker 3: compiles without actually building it, you can use cargo check.
- 4:13Speaker 2: It's lightning fast for that.
- 4:14Speaker 1: Okay, let's shift from the tools to the language itself.
- 4:17Speaker 1: Let's talk about variables. The syntax looks pretty familiar, you know,
- 4:20Speaker 1: curly braces, semicolons.
- 4:23Speaker 3: The designers definitely wanted it to feel approachable if you're
- 4:26Speaker 3: coming from C or Python or JavaScript.
- 4:29Speaker 1: So you declare a variable with the let statement, something
- 4:32Speaker 1: like let price equals twenty five.
- 4:34Speaker 2: Right.
- 4:34Speaker 3: And even though this is a strongly typed language, you
- 4:37Speaker 3: notice we didn't have to write, you know, I thirty
- 4:39Speaker 3: two for integer.
- 4:40Speaker 1: That's type inference. The compiler just figures it out for
- 4:43Speaker 1: you exactly.
- 4:43Speaker 2: It saves you typing, but it's still just.
- 4:45Speaker 1: District and let. Fain has this cool trick called destructuring.
- 4:49Speaker 1: You can use a pattern like a tipple to initialize
- 4:51Speaker 1: a bunch of variables at.
- 4:52Speaker 3: Once, which is incredibly handy when you're getting multiple values
- 4:56Speaker 3: back from a function.
- 4:57Speaker 2: It's very concise.
- 4:58Speaker 1: Now, this brings us to what it might be the
- 5:00Speaker 1: most important foundational idea of the language, immutability by default.
- 5:06Speaker 1: Variables are immutable by default, which is weird for a
- 5:10Speaker 1: lot of people.
- 5:11Speaker 3: It is very unusual. Most languages assume variables will change
- 5:16Speaker 3: unless you tell them not to. This is the complete opposite.
- 5:19Speaker 1: But wait a second, if everything is immutable, isn't that
- 5:23Speaker 1: just a massive pain? Like how do you even do
- 5:26Speaker 1: something simple like incremental counter?
- 5:28Speaker 3: That is the number one question everyone asks, and the
- 5:31Speaker 3: answer is that the compiler forces this on you for
- 5:33Speaker 3: three huge benefits, safety, concurrency.
- 5:37Speaker 2: And speed.
- 5:37Speaker 1: Okay, break goes down.
- 5:39Speaker 3: For safety, if a value can't change, you eliminate a
- 5:41Speaker 3: whole category of bugs from unexpected side effects. For concurrency,
- 5:46Speaker 3: this is massive data that's immutable can be shared across
- 5:49Speaker 3: multiple threads with no need for complex lock.
- 5:52Speaker 1: So you just bypass that entire nightmare of mutex is
- 5:55Speaker 1: and race conditions because the compiler can prove it's safe.
- 5:58Speaker 3: To share precisely, which leads directly to speed. And the
- 6:01Speaker 3: compiler also knows that since the data is static, it
- 6:04Speaker 3: can perform all sorts of aggressive optimizations. So immutability is
- 6:08Speaker 3: the default because it's the safest and often the fastest
- 6:11Speaker 3: state for data to be in.
- 6:12Speaker 1: But obviously we do need to change things sometimes, of course.
- 6:16Speaker 3: And for that you have to be explicit. You opt
- 6:19Speaker 3: into mutability with the MUTT keyword. Let mut score equals
- 6:23Speaker 3: thirty two.
- 6:24Speaker 1: And if you forget that mut and try to change it, the.
- 6:26Speaker 3: Compiler stops you cold, but it gives you a really
- 6:29Speaker 3: nice error message pointing to the line and literally suggesting
- 6:33Speaker 3: you add mutt. It's like a helpful guide, not just
- 6:35Speaker 3: an enforcer.
- 6:36Speaker 1: Now you mentioned there's something even more immutable than a
- 6:38Speaker 1: default let variable, the.
- 6:40Speaker 2: Even immutabler one. Yeah, the constant. You declare it with
- 6:43Speaker 2: the const keyword.
- 6:44Speaker 1: And it has some very strict rules, four of them.
- 6:47Speaker 3: First, you have to use constant. Second, the name has
- 6:49Speaker 3: to be in what's called screaming snake case.
- 6:52Speaker 1: Screaming snake case.
- 6:53Speaker 2: All caps with underscores.
- 6:55Speaker 3: It really stands out, It certainly does.
- 6:57Speaker 2: Third, you must.
- 6:57Speaker 3: Provide a type annotation no zo type inference for constants.
- 7:02Speaker 3: And fourth, the value has to be something that can
- 7:04Speaker 3: be calculated entirely at compile time. A simple number, a string, literal,
- 7:08Speaker 3: something like that.
- 7:08Speaker 1: So why go through all that hassle for a cost?
- 7:11Speaker 1: If an immutable let works.
- 7:13Speaker 3: Two big reasons. First, you can declare them at a
- 7:16Speaker 3: global scope, outside of any functions, so they're accessible everywhere.
- 7:20Speaker 3: And second, the compiler actually inlines them. It replaces every
- 7:25Speaker 3: use of the constant with its actual value directly in
- 7:28Speaker 3: the machine code.
- 7:28Speaker 1: Which makes them incredibly fast at runtime, no variable look
- 7:32Speaker 1: up at.
- 7:32Speaker 2: All, zero overhead.
- 7:34Speaker 1: All right, let's talk about organization. Let's talk about scope.
- 7:37Speaker 3: Scope is just the area of the code where a
- 7:39Speaker 3: variable is valid. It starts where you create it and
- 7:42Speaker 3: it ends at the closing curly brace of its block.
- 7:44Speaker 1: And this is where the memory management comes in. There's
- 7:47Speaker 1: no garbage collector nope.
- 7:49Speaker 3: When a value goes out of scope, its memory is
- 7:51Speaker 3: immediately dropped freed. It's deterministic and predictable, which is fantastic
- 7:57Speaker 3: for performance.
- 7:57Speaker 1: So if you try to use a variable after its
- 7:59Speaker 1: scope end, it.
- 8:00Speaker 3: You get a compile time error cannot find value x
- 8:03Speaker 3: in this scope. No runtime surprises, the compiler just won't
- 8:07Speaker 3: let you make that mistake.
- 8:08Speaker 1: There's another really powerful feature related to scope, and that's shadowing.
- 8:12Speaker 3: Yes, shadowing is really idiomatic in this language. It lets
- 8:16Speaker 3: you declare a new variable with the exact same name
- 8:19Speaker 3: as an existing one.
- 8:21Speaker 1: And then new one hides the old one for a.
- 8:22Speaker 3: While, exactly until the new one goes out of scope.
- 8:25Speaker 3: It's super useful for data transformations. You can, for instance,
- 8:30Speaker 3: shadow a mutable variable with an immutable one once you're
- 8:33Speaker 3: done changing it, locking it.
- 8:34Speaker 1: Down or and this is really cool You can even
- 8:37Speaker 1: use it to change the variables type.
- 8:38Speaker 3: Mm hmm.
- 8:39Speaker 2: This is a great pattern.
- 8:41Speaker 1: So say I have user input as text, I call
- 8:43Speaker 1: it user input. I can then immediately create a new variable,
- 8:47Speaker 1: also called user input that holds the integer version of
- 8:49Speaker 1: that text.
- 8:50Speaker 3: Right, and you don't have to clutter your code with
- 8:53Speaker 3: names like impuede hastring and impute ass number. You just
- 8:56Speaker 3: keep the same logical name user input as it transforms
- 8:59Speaker 3: it very clean.
- 9:00Speaker 1: Okay, let's get to the main building blocks of any program.
- 9:03Speaker 1: Functions you define them with the fn.
- 9:05Speaker 3: Keyword, and the convention is snake case, so all lowercase
- 9:08Speaker 3: with underscores, like calculating some A.
- 9:11Speaker 1: Nice feature here is that you don't have to declare
- 9:13Speaker 1: functions before you use them. You can put your main
- 9:16Speaker 1: function right at the top of the file.
- 9:17Speaker 3: Yeah, the compiler doesn't care about the order, which lets
- 9:20Speaker 3: you organize your code logically instead of being forced into
- 9:23Speaker 3: a certain structure.
- 9:24Speaker 1: When you define one, the parameters are always name.
- 9:27Speaker 3: Type and the return type comes after an arrow myotype.
- 9:31Speaker 1: And while you can use the return keyword.
- 9:33Speaker 3: The idiomatic way, the preferred style is to use a tail.
- 9:37Speaker 1: Expression, meaning you just lead a semicolon off the last
- 9:40Speaker 1: line of the function yep.
- 9:42Speaker 3: That expression's value is then automatically returned. It leads to
- 9:46Speaker 3: a much cleaner, more expression oriented style of coding.
- 9:49Speaker 1: Function calls look totally standard, but if you see something
- 9:53Speaker 1: that looks like a function call but ends with an
- 9:54Speaker 1: exclamation mark like print.
- 9:57Speaker 3: One, that's a macro. They look similar, but they're much
- 10:00Speaker 3: more powerful and can do things standard functions can't.
- 10:03Speaker 1: Okay, let's circle back to that core promise of safety.
- 10:06Speaker 1: The language guarantees memory safety at compile time, and a
- 10:11Speaker 1: huge part that is that variables must be initialized before
- 10:14Speaker 1: you use them.
- 10:15Speaker 3: This is not negotiable for the compiler. If there is
- 10:18Speaker 3: any path through your code where a variable could be
- 10:21Speaker 3: used before it's given a value, you get an error
- 10:24Speaker 3: use of possibly uninitialized variable.
- 10:27Speaker 1: And here's the example that I think really defines the
- 10:29Speaker 1: whole philosophy. If you try to initialize a variable inside
- 10:33Speaker 1: an if statement, even if the condition is just if.
- 10:37Speaker 3: True or if one equals one.
- 10:39Speaker 1: Right, something that is obviously always going to run, the
- 10:41Speaker 1: compiler will still reject it.
- 10:43Speaker 3: Why because the compiler is not designed to reason about
- 10:47Speaker 3: the logic of the condition at runtime. It only looks
- 10:50Speaker 3: at the structure of the code and the structure of
- 10:52Speaker 3: an if statement has two possible.
- 10:54Speaker 1: Paths, even if one path is impossible.
- 10:56Speaker 3: Even then, if one of those paths doesn't initialize the variable,
- 11:00Speaker 3: the guarantee of safety is broken, so it rejects the code.
- 11:03Speaker 3: It insists on guaranteed safety, not probable safety.
- 11:06Speaker 1: That's such a contrast to a language like C, where
- 11:09Speaker 1: using an uninitialized variable is just uncooing behavior, which is terrifying.
- 11:13Speaker 3: It could crash, it could work fine, it could leak
- 11:15Speaker 3: secret data.
- 11:16Speaker 2: You have no idea.
- 11:17Speaker 3: This language just eliminates that entire category of problem.
- 11:20Speaker 1: Speaking of reliable code, let's touch on the module system.
- 11:23Speaker 1: Every project has a root like main dot rs, and.
- 11:26Speaker 3: Can also have a library route live dot rs. And
- 11:29Speaker 3: the default state here is again about safety privacy.
- 11:33Speaker 1: Everything is private by default.
- 11:35Speaker 3: Everything, even if it's in the same project. If you
- 11:37Speaker 3: want a function to be visible outside its own file,
- 11:40Speaker 3: you have to explicitly mark it as public with the
- 11:44Speaker 3: pub keyword.
- 11:45Speaker 1: And once it's public, how do you access it.
- 11:47Speaker 2: You could use the.
- 11:48Speaker 3: Full path like project name, not module dot function, but
- 11:51Speaker 3: that can get.
- 11:51Speaker 2: Really long, so you use the use statement exactly.
- 11:54Speaker 3: Use brings that item into your current scope, so you
- 11:57Speaker 3: can just call the function directly. It's basically like an
- 11:59Speaker 3: import statement in other languages.
- 12:01Speaker 1: And what about getting code from outside your project entirely
- 12:04Speaker 1: external packages?
- 12:05Speaker 3: Those live on creates dot io, the public registry. You
- 12:09Speaker 3: find the one you want, and you just add a
- 12:10Speaker 3: line to your cargo dot tommlfile under the dependency section
- 12:13Speaker 3: something like package name a version. Cargo does all the
- 12:16Speaker 3: work of downloading and compiling it for you.
- 12:18Speaker 1: What a journey We've gone from setting up a project
- 12:21Speaker 1: with cargo to defining data with this really strict but
- 12:24Speaker 1: safe default immutability.
- 12:26Speaker 3: All the way to how the compiler guarantees memory safety
- 12:29Speaker 3: through these really rigorous checks at compile time.
- 12:32Speaker 1: The whole theme is just predictability, preventing errors before they
- 12:36Speaker 1: can ever happen.
- 12:36Speaker 3: Exactly, the compiler has to be guaranteed of safety before
- 12:40Speaker 3: it will produce a program. That's the core principle. It
- 12:42Speaker 3: saves so so much time in debugging.
- 12:45Speaker 1: Absolutely so. As a final thought for you, the listener
- 12:48Speaker 1: to chew On, we talk about how shadowing lets you
- 12:51Speaker 1: redefine a variable, even changing its type like from text
- 12:54Speaker 1: to a number. Given this intense focus on compile time safety,
- 12:58Speaker 1: how could you use that shadowing feture to build a
- 13:00Speaker 1: safe data pipeline. Think about taking untrusted user input and
- 13:04Speaker 1: step by step shadowing it as you validate and transform
- 13:08Speaker 1: it into its final safe type, all without creating a
- 13:11Speaker 1: mess of intermediate variables. That's a key to writing clean, safe,
- 13:14Speaker 1: idiomatic code.