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:

TypeScript logo over a pink and white background.

Drizzle vs. Prisma: Which ORM is best for your project?

Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.

Temitope Oyedele
Nov 21, 2024 â‹… 10 min read
Practical Implementation Of The Rule Of Least Power For Developers

Practical implementation of the Rule of Least Power for developers

It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.

Timonwa Akintokun
Nov 21, 2024 â‹… 8 min read
Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 â‹… 13 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