Explore the new React ViewTransition, addTransitionType, and Activity APIs by building an AirBnB clone project.
Compare gRPC vs REST to understand differences in performance, efficiency, and architecture for building modern APIs.
The switch to Go may be a pragmatic move in the short term, but it risks alienating the very developers who built the tools that made TypeScript indispensable in the first place.
Discover the basics and advanced use cases of type casting, how and why to use it to fix type mismatches, and gain some clarity on casting vs. assertion.
2 Replies to "How to define higher-order functions in Rust"
Thanks for a great article. In python I can pass a method that is bound to an object. Can I do the equivalent in rust?
Hello Giles. A method in Rust is just a function, which also takes a first `self` parameter (similarly to Python). Hence, passing a method is just a matter of using the right types in the function signatures:
pub struct Greeter {
greeting: String
}
impl Greeter {
fn greet(&self, name: String) -> String {
return format!(“{}, {}”, self.greeting, name);
}
}
fn call(name: String, fun: fn(&Greeter, String) -> String) -> String {
let greeter = Greeter { greeting: “Hello”.to_string() };
return fun(&greeter, name);
}
fn main() {
println!(“{}”, call(“Giles”.to_string(), Greeter::greet));
}