2023-02-14
1111
#rust
Matteo Di Pirro
158647
107
Feb 14, 2023 â‹… 3 min read

How to define higher-order functions in Rust

Matteo Di Pirro I am an enthusiastic young software engineer who specialized in the theory of programming languages and type safety. I enjoy learning and experimenting with new technologies and languages, looking for effective ways to employ them.

Recent posts:

The 10 Best React Native Component Libraries You Should Know

The 10 best React Native UI libraries of 2025

UI libraries like React Native Paper and React Native Elements offer pre-developed components that help us deliver our React Native projects faster.

Aman Mittal
Feb 21, 2025 â‹… 7 min read
top ten docker alternatives worth considering

The 10 best Docker alternatives to consider

Although Docker remains the dominant platform for containerization and container management, it’s good to know about different tools that may work better for certain use cases.

Ayooluwa Isaiah
Feb 21, 2025 â‹… 13 min read
how to use the ternary operator in javascript

How to use the ternary operator in JavaScript

Add to your JavaScript knowledge of shortcuts by mastering the ternary operator, so you can write cleaner code that your fellow developers will love.

Chizaram Ken
Feb 21, 2025 â‹… 7 min read
Using tsup To Bundle Your TypeScript Package

Using tsup to bundle your TypeScript package

Learn how to efficiently bundle your TypeScript package with tsup. This guide covers setup, custom output extensions, and best practices for optimized, production-ready builds.

Muhammed Ali
Feb 20, 2025 â‹… 7 min read
View all posts

2 Replies to "How to define higher-order functions in Rust"

    1. 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));
      }

Leave a Reply