A deep dive into the Liskov Substitution Principle with examples, violations, and practical tips for writing scalable, bug-free object-oriented code.
This article walks through new CSS features like ::scroll-button()
and ::scroll-marker()
that make it possible to build fully functional CSS-only carousels.
Let’s talk about one of the greatest problems in software development: nascent developers bouncing off grouchy superiors into the arms of AI.
Flexbox and Grid are the heart of modern CSS layouts. Learn when to use each and how they help build flexible, responsive web designs — no more hacks or guesswork.
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));
}