Has Rust considered something like Scala's 'given' arguments?

⚓ Rust    📅 2026-02-07    👤 surdeus    👁️ 1      

surdeus

Lately I’m writing a lot of code with “context” arguments. This seems to happen more in Rust, because the language does not like object-graph soup. Objects in a tree can’t contain up/parent pointers as easily as, say, Java. So code that looks like this in another language:

button.render()
label.render()

becomes something like this in Rust:

let ctx: RenderCtx = ...
button.render(&ctx);
label.render(&ctx);

I haven’t used Scala in years, but I remember Scala version 2 had “implicits” and now version Scala 3 has something similar with the keywords ‘given’ and ‘using’. Rustified, it would look something like:

let given ctx: RenderCtx = ...
button.render()
label.render()

impl Button {
    fn render(&self, using ctx: &RenderCtx) { ... }

So a value in the static scope of a call, marked a certain way, like ctx here, becomes available to implicitly pass for certain parameters.

I’m curious if this is, or ever was, a proposal for Rust.

6 posts - 5 participants

Read full topic

🏷️ Rust_feed