Check out a free preview of the full The Rust Programming Language course:
The "Strings" 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 discusses string interpolation in Rust including how to print and return string variables to the terminal using println, format, and panic. The panic command is only at the end of a program used when the program is finished running or crashes to print a message to terminal.

Get Unlimited Access Now

Transcript from the "Strings" Lesson

>> All right, so in this section we're going to talk about strings, floats, integers, and Booleans. And along the way, we'll talk about a few like different basic language constructs that sort of fit around these things. Okay, so here is hello world in Rust. We say fn main.

[00:00:15] So this is how you declare a function in Rust, is what the fn keyword, main is the name of our function. Doesn't take any arguments. And then inside the curly braces are the body of the function. So far, this is probably looking pretty similar to what you might expect from a language like JavaScript, except for this exclamation point.

[00:00:30] We'll get back to that exclamation point in a couple of slides. This is something that you might put inside a file called, let's say is Rust's file extension. And then I would compile this file by running rustc When I run that, it's going to give me out a binary file, which is something like app.exe on Windows or just something called app on Mac OS or Linux.

[00:00:54] Then I can just execute that file normally and it will print out Hello, World to the screen. So this right here is the entirety of Hello World in Rust. Rust has string interpolation, but not built in as a language feature like you might find it some languages like Python or Ruby.

[00:01:08] It's a little bit different in Rust. So in Rust, this string interpolation that I'm doing right here, where I'm saying print line and then I have these curly braces, and the first curly brace is going to get substituted with greeting, which in this case is hello. And the second curly brace is going to get substituted with subject, which in this case is world.

[00:01:25] So this is going to print out hello world just like the version we had before. But it's doing so with this sort of string interpolation technique. Now, the difference here is that in Rust, it's not actually that all strings support this, but rather that print lines specifically supports this as part of its implementation.

[00:01:43] So although this is going to print hello world, just like before, it's it's doing a little bit differently. Print line is not the only way that you can do string interpolation. Another really useful way to do string interpolation is with this format. So the difference between format and print line is print line, although it does string interpolation, it always prints it out to the console.

[00:02:03] Whereas format will just take this interpolation and then do the substitutions in the same way that we had before, but it'll just return a string. So you can use this to sort of compose together different strings using string interpolation. Another thing that uses string interpolation is panic. So panic right here, as we can see, it's using string interpolation to put the crash_reason inside the panic message.

[00:02:28] And panic is basically when you want to end the program. So panic is not like throw in a lot of languages or raise or something like that, it's not based around exceptions. It's really saying panic means like we're done. I give up. I can't keep running this program, we're exiting right now.

[00:02:43] Technically there are ways that you can recover from panics in Rust, but it's kind of convoluted and it's, in practice it's really not done. Basically when you panic, you're saying like I'm done. This program is completely finished. That's why I have this line greyed out right here is that this line will never get run.

[00:02:56] There's no way to really get this line to run because we're going to have panicked before then and in fact, the compiler will give you a warning about this saying like, hey, you have some code after a panic. That's not actually going to gonna run anything.