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:

Master JavaScript Date And Time: From Moment.js To Temporal

Master JavaScript date and time: From Moment.js to Temporal

JavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.

Yan Sun
Dec 4, 2024 â‹… 9 min read
Npm Vs. Npx: What’s The Difference?

npm vs. npx: What’s the difference?

Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.

Fimber Elemuwa
Dec 3, 2024 â‹… 5 min read
How To Audit And Validate AI-Generated Code Output

How to audit and validate AI-generated code output

Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.

Boemo Mmopelwa
Dec 2, 2024 â‹… 5 min read
Building A Background Remover With Vue And Transformers.js

Building a background remover with Vue and Transformers.js

Build a real-time image background remover in Vue using Transformers.js and WebGPU for client-side processing with privacy and efficiency.

Emmanuel John
Nov 29, 2024 â‹… 9 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