Write and call a function in Rust

Pascal Precht
InstructorPascal Precht
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson we'll create a new function that receives parameters and called inside our program's main() function.

Pascal Precht: [00:01] Let's say we want to create a function that takes a first name and a last name and prints it to the screen. To define a function, we use the fn keyword followed by a name, and then we define the parameters, in our case it's the first_name of type string and the last_name of type string as well.

[00:22] We then define the function body, which is going to be println! With a placeholder and another placeholder followed by the first_name and last_name. This function takes a first_name, which is of type string, and a last_name, which is of type string, and will then output the first_name and the last_name using println! And the placeholder syntax.

[00:52] We can then go ahead and call this function by using same name inside our main function. We pass it first and last. Save the file and run the program. We'll see it outputs "Pascal Precht."

Michele Nasti
Michele Nasti
~ 4 years ago

Why do we have to use the .to_string() method call? What is the type of "Pascal" ? I see from the compiler that it's &str, how does it work under the hood?

Pascal Precht
Pascal Prechtinstructor
~ 4 years ago

Hey Michele!

This is an excellent question. So as you've already pointed out "Pascal" is of type &str which is a "string slice". A string slice is preallocated readonly memory that ships with the program.

A String, which is the type you get when you call .to_string() on a &str, is a pointer type that allocates the text on the heap.

This may or may not make too much sense right now, because a good basic understanding of how Rust manages memory is needed for that. I will cover this in future videos, but for now I've put together an article that explains exactly that:

https://twitter.com/PascalPrecht/status/1234833467239731205

J. Matthew
J. Matthew
~ 4 years ago

This is an excellent question. So as you've already pointed out "Pascal" is of type &str which is a "string slice". A string slice is preallocated readonly memory that ships with the program.

A String, which is the type you get when you call .to_string() on a &str, is a pointer type that allocates the text on the heap.

Huh, that's really interesting. I was wondering about @Michele's question, too. I would have guessed that &str was itself a pointer based on the ampersand, but it sounds like it's slightly different. I'm not super-strong on pointers, though. I'll check out that article; thanks for putting it together!

Pascal Precht
Pascal Prechtinstructor
~ 4 years ago

I would have guessed that &str was itself a pointer based on the ampersand, but it sounds like it's slightly different.

This is very correct. I probably expressed myself not correctly. So here's the deal:

  • A String is a pointer type that, in memory, stores the pointer, length of the text and capacity on the stack, while the actual text is being stored on the heap in a buffer.
  • A str is a slice, or substring of either such data in a buffer or preallocated memory. But because the data lives either on the heap or the preallocated memory of the program, you always need a reference to access the data
  • A &str is a pointer to a str. As mentioned above, a &str is a reference that points to a str which either lives in a buffer (and represents an entire or substring of a String), or in preallocated memory.

I know this is tough to grasp (trust me, took me a while too), so I've written this article that aims to help:

https://blog.thoughtram.io/string-vs-str-in-rust/

J. Matthew
J. Matthew
~ 4 years ago

I know this is tough to grasp (trust me, took me a while too), so I've written this article that aims to help

Thanks, I read that article and Rust's documentation on Ownership/slices, and I get it now.

Markdown supported.
Become a member to join the discussionEnroll Today