Check out a free preview of the full The Rust Programming Language course:
The "Primitives Recap and Q&A" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard provides a brief overview of information covered in the first section of the course and opens for student questions. Student questions regarding the difference between traits and macros in Rust, if a return type can be inferred, how to check for libraries that Rust has, and if there are ternary operators are covered in this segment. Additional questions covered include if Rust is compiled into bit code or if it's generated directly into machine code, can binary be cross compiled, if there are queue and stack collections, if a new I64 is created when using something like y as I64, are monoids and monads used in Rust, and if there is any built in crypto.

Get Unlimited Access Now

Transcript from the "Primitives Recap and Q&A" Lesson

>> Okay, so let's review part 1 and then we'll ask open it up for any general questions about anything we've talked about in the whole section. So we started out talking about strings, so this is an example of using the format macro to say hi, comma and then the curly braces the name will get substituted in the curly braces.

[00:00:16] We also talked about print line and panic being to met macros that also support string interpolation but again, that's built into those particular macros. It's not like a general language concept, talks about floats, so here's an example of a float that I've defined with let mut. So it's mutable, which means I'm allowed to redefine it later on if I want and I've got my decimal point there, which is how we know it's a float.

[00:00:36] I've also optionally chosen to do a type annotation to say, this is an f64, but I also could have made an f32 or I could have left it off and just let rust infer it instantly. If you're curious by default, it will default to f64, but that's just sort of an implementation detail, usually, the compiler has more information it'll sort of figure out what you want to.

[00:00:56] Integers, we have let one, it has the type u32, so this is not a mutable let, and here I've converted a float 1.99 as u32. The reason I call this one is because when you convert to a float to a u32, it's just gonna drop the decimal points, also, if you're curious, by defaults if I didn't put this on here, right?

[00:01:19] Rust would actually default to i32 as the default integer type and finally, we talked about booleans, so here's an example of using, if I said x greater than 5, then true else false. And because I didn't put any like semi colons anywhere, if I wanted to this. I could replace this true or this false with something more complicated than at the end have an expression there, which is what this entire expression would evaluate to.

>> What's the difference between traits and macros in rust?
>> Great question, difference between traits and macros, basically, they have nothing to do with each other, so macros are like, we discussed something that happens purely at compile time. That's something like, syntax sugar where When I call format right here, the ad compile time.

[00:02:04] Rust is going to execute some code that's inside that format macro, which is going to expand it into a series of other function calls. In contrast, traits are something more similar to something like classes, it's more about like it's called dispatch. But basically trying to figure out which of several functions should be called in a particular point, and it's a way to have things like code reuse, and like some additional flexibility.

[00:02:29] So really, macros and traits do totally different jobs, we're actually traits is one of the several things that are part of the rust language that we're not gonna get into in this course. But it trades should be something relatively familiar based on other languages that you've used, like interfaces or.

[00:02:48] I guess they're not quite like interfaces, depending on your language but, yeah, we won't quite get into those in the scope of this course.
>> Can I return type B inferred?
>> Can a return type B inferred, not for functions, they can for closures, but also closures are something we won't get into in this course, closures are sort of like inline, lambdas, inline functions.

[00:03:13] And for those that can actually infer both the arguments and the return type but for normal functions, which is all we're going to cover in this course, it. You have to write out the entire type including the return type.
>> Does rust have DSA library like CPP?
>> DSA, I don't actually know what DSA stands for but if you want to check, so if you go to, that's rust's package registry.

[00:03:39] So you can do a search in there and it'll tell you all the libraries that it does have crates is kind of like NPM for rust. And basically, it will tell you all the packages that you can find and maybe you can find something that's the equivalent of what you're looking for there.

>> Is there's something like a ternary operator in rust?
>> Great question, is there something like a ternary operator in rust, yes, but we've already learned it. Because if, when used as an expression is basically exactly the same thing as a ternary operator, so rust doesn't have a separate syntax for that.

[00:04:06] It just lets you use if like an expression, basically exactly like a ternary if you want to.
>> Is rust like Java, compiled into byte code and run on a VM or does it generate machine level code for the platform?
>> Great question, yeah, it just goes straight to machine code, we had a slide on this very back at the beginning here.

[00:04:26] But basically, yeah, rust just goes straight to either machine code and a binary executable or to web assembly, there is no intermediate bytecode.
>> Yeah, can we cross compile binaries compiling for Mac, Linux, windows and is there an easy way to do that?
>> Got it, question was can you cross compile binary, so for example, I'm running on a Mac here, can I use my Mac to compile a rust binary not only to Mac binaries.

[00:04:51] But also to Windows and Linux, not currently, so if I want to build for Window. I would need to actually like run my rust compiler on my rust code on a Windows machine, same thing for Linux or on a VM is much more common.
>> Are there queue and stack collections in rust?

>> Yes, are there queue and stack collections, there are and again, I would refer you to to find a whole selection of them.
>> When doing y as i64 does rust copy the original value to a new i64 value in memory?
>> When do, yes, when doing like y as i64 does rust copy that i64 to a new i64, there is a copy, whatever that number was to a new i64 memory, yes.

[00:05:35] It would have to be because it's a different size, those are all going to end up CPU registers, so, but also, it's also extremely fast, like the, AS is also trively fast. Monoids and monads are practically rust, no, rust does not have higher kind of types. So if you're, for any Haskell fans in the audience, those aren't things that that rust really has any kind of reasonable support for.

[00:06:02] If you don't want to learn what monads and monoids or maybe that's a good thing.
>> Does rust have any built in crypto?
>> Does rust have any built in crypto, I, don't they, I mean certainly not built into the language. But definitely there's plenty of cryptography libraries out there, so just depends on your definition of built in.