Rust By Example: Functions and Methods
ฝัง
- เผยแพร่เมื่อ 8 ก.พ. 2025
- Functions in Rust are marked with the keyword FN. They can be either module-level, meaning available anywhere in your file, or associated with something like a structure or data type. The arguments of the functions are somewhat similar to variables, but they need to be annotated with a type, such as String, Struct Type, or a number. For a function that returns a value, we need to specify the return value type with an arrow.
Sometimes return values can be tricky, because in Rust the output could be the last expression in your function. This is a bit similar to languages like Perl. Alternatively, you could use a return statement.
One quirky rule in Rust is that if you end with a semicolon, your function is going to return null, which can be rather confusing. We can write a function that basically does nothing. It usually takes data as an argument, performs an operation, and then returns a value. if you prefer, you can create a function that simply executes a statement without returning anything at all.
Rust allows such flexible function signatures. Rust treats related functions somewhat differently compared to some other programming languages. You can define static functions and methods associated with your data structures.
In Rust, you have your "associated functions", which are similar to static functions in other languages. They can be used at any time, whether you have a struct instance or not. On the other hand, you have methods, which need an instance of a struct to be used.
They can mutate the state of the struct instances or just read from it. Rust allows you to use all these different types of functions and methods for a lot of flexibility. You can also write a method that changes the values in your struct.
You use the keyword "mut" to make your struct mutable, that is, able to be changed. There's also a way to let Rust know that your variable is no longer needed so it can clean up, or "destroy", the function, and release its resources. The Rust compiler automatically handles these situations where your variables go out of scope, freeing up your system's resources.
Lastly, just like in C and C++, there's no specific order in which the functions should be defined in Rust. It's all handled by the compiler which makes sure everything is in the right place before the program runs. This can be especially helpful when you're working on a big codebase and a function definition might get buried deep in the code.
In Rust, you'll often return the data of your functions instead of printing to the standard output. This is a good pattern for code portability and is useful when writing code meant to be used by others. These are just some of the things you can do with functions in Rust.
The language is always flexible and modular, giving you a lot of room to work with when you're writing your code. You can write code that's convoluted and complex or keep it simple and straightforward, depending on what best suits your needs.