Categories > Coding > Rust >
Fun rust features
Posted
Hello guys, 0x90 here.
Today I'm going to show you some of the coolest features of rust and explain how they work.
1. Macros
Rust has 2 types of macros, declarative and procedural macros; since they're easier I'll start off showing you declarative macros and the syntax they possess.
https://cdn.discordapp.com/attachments/873655916212412439/1071938518839992380/Snap.png
This is a declarative macro, it's defined by the macro_rules! keyword and the name comes after, when called a ! is suffixed to distinguish it from normal functions.
If you noticed, the body of the macro code follows a match like syntax, this is because declarative macros are based on patterns and can be overloaded.
If the code that you pass to a macro matches one of the patterns in its definition's body then it'll expand into that, let me show you.
https://cdn.discordapp.com/attachments/873655916212412439/1071939757803851876/Snap.png
In this code, the $ identifies the name of a parameter, and the : expr denotes that the type of this parameter will be an expression.
You can also embed patterns into macro parameters and if an invocation matches the pattern specified by one of the overloads it will expand into its body.
2. Iterators
In rust, iterators are very powerful, you can use their standard iterator trait and make a universal iterator over any type you want with ease.
Let me show you some of the base rust iterators and what they can do.
https://cdn.discordapp.com/attachments/873655916212412439/1071953646914379856/Snap.png
This code builds a range based iterator from 1 to 100, enumerates all elements in the range and adds them together to get a sum of all values from 1 to 100, outputting 5050.
https://cdn.discordapp.com/attachments/873655916212412439/1071954414920806510/Snap.png
In this example, we get a list of vowels and take an input string, get an iterator over all characters of the string, rebuild the iterator filtered out to only the vowels in the string, then collect these vowels into an entirely new string.
https://cdn.discordapp.com/attachments/873655916212412439/1071955534506381402/Snap.png
Here, we again build a range based iterator (from 0 to 9), the map function then goes through every element of this iterator and constructs a new iterator from it, within here we use the rand crate and the sample function to generate a random character per number in the range, then we collect it into a string, giving us a 10 letter random string.
For today, that's all I'll show you but I might make this something I do more often - 0x90.
Added
@_realnickk Here's some code showing you 3 different ways to concatenate 2 things in rust.
https://cdn.discordapp.com/attachments/873655916212412439/1071966226860998696/image.png
And here's what those macro invocations expand into
Cancel
Post
Added
@_realnickk You can have a copy in rust, they just aren't implicit. To copy a string you must do s.clone().
The reason you couldn't return a reference to the string is because it was probably a temporary.
You can't export a reference to a temporary value outside of its scope, return a value instead.
fn do_stuff(x: &String, y: &str) -> String {
x + y
}
Cancel
Post
Replied
Another fun rust feature added onto this would probably be Ownership & Borrowing for mem management. I like how the lang innovates with it
Cancel
Post
20/2/23
Replied
The syntax looks a bit edgy. I suppose I'll attempt to learn rust.
Cancel
Post
Replied
@85021 you can use rust for anything.
If you want to make games check out Bevy.
As for UI development, a nice library is egui (the website is written in rust as well.)
You can literally use rust for anything.
Games, UIs, drivers, 3ds programs, front ends with wasm and backends too, you can even use it with roblox
Cancel
Post
Replied
rust is weird /chars
Cancel
Post
Random quote here...
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post