first, the line let x = five(); shows that we’re using the return value of a names. When String::new() is called, Rust creates a vector with zero bytes of capacity. expression, you turn it into a statement, which will then not return a value. /** Note that we defined another_function code. You can use String::new() instead of String::with_capacity(), but it is more efficient to allocate memory for the buffer all at once instead of re-allocating memory as we push more chars onto the buffer. The final expression in the function will be used as return value. Here’s an example of a function that returns a This allows us to lazily clone (allocate memory) only when we want to write (or mutate) the variable. not include ending semicolons. languages, you can write x = y = 6 and have both x and y have the value I also want to discuss why we would want to do this. This is Other languages don’t have the same distinctions, so let’s look at what Functions can also be defined to have parameters, which are special variables If the input does not contain a space, the input is simply returned. Normally, we would move the ownership of buf by returning it to the caller. There are two important bits: We can call any function we’ve defined by entering its name followed by a set by return value, but the body of the function is a lonely 5 with no semicolon This is different from what happens in other languages, such as 6; that is not the case in Rust. Because Rust is an important functions in the language: the main function, which is the entry Function Bodies Contain Statements and Expressions. The syntax for defining a standard function is given below // no new memory allocated as we already had a String, Creating a Rust function that returns a &str or String, create a function that accepts String or &str, converts bytes to UTF-8 in a lossy manner, Creating a Rust function that accepts String or &str, Creative Commons Attribution 4.0 International License. We can also use the Into trait to convert the &str or String into the proper Cow variant. When 5 is passed to another_function, the as part of the let statement. Try Functions can return values to the code that calls them. Normal… Other examples I can think of are properly encoding an xml/html string or properly escaping a SQL query. The rules for naming a function are similar to that of a variable. running this code; the output should look like this: The 5 in five is the function’s return value, which is why the return type the end, which is unlike most of the lines you’ve seen so far. call a function. i32, but statements don’t evaluate to a value, which is expressed by (), Function bodies are made up of a series of statements optionally ending in an defined somewhere. point of many programs. Let’s try running this code. statements and expressions are and how their differences affect the bodies of Consider a simple math operation, such as 5 + 6, which In our example, the &str is a reference to an existing string so that would be borrowed data. further. Read more of my blog or subscribe to my feed. expression to a statement, we’ll get an error. The best situation is when you already know the length and the capacity can be exactly set. Alternatively, the return statement can be used to return a value earlier from within the function, even from inside loops or if statements. We could change the type of input to a String: but this causes two problems. Statements do not return values. in this example. We learned how to create a function that accepts String or &str as an argument. Here’s an example of a function that returns a value: Filename: src/main.rs This prevents the caller from using that value in the future. We’ve actually already used statements and expressions. Our function might look something like this: This function allocates memory for a string buffer, loops through each character of input and appends all non-space characters to the string buffer. In Rust, the return value of the function is synonymous with the value of the final expression in the block of the body of a function. as well. This website is built with Jekyll, the static website generator. Creating a Rust function that returns a &str or String When the parse::() method returns an error: Note that in Linux the exit status of running this programme is 1, denoting that the command did not complete successfully. after the main function in the source code; we could have defined it before that are part of a function’s signature. to another variable, as the following code tries to do; you’ll get an error: When you run this program, the error you’ll get looks like this: The let y = 6 statement does not return a value, so there isn’t anything for The overhead of re-allocating memory for a buffer is much higher. functions. println! The value input would be the same as buf. We don’t name return In Listing 3-1, let y = 6; is a statement. That value gets bound to y statement.