Why must TryInto consume? (The Refactoring Blues)

⚓ rust    📅 2025-05-12    👤 surdeus    👁️ 2      

surdeus

Scenario: I want to convert a vendor-specific "message" type (call it Message) to a neutral domain message type.

I implement TryFrom - idiomatic Rust.

A function take a &Message and calls try_into with it.

Does not compile - try_into consumes its value.

Of course, I can clone the message. But I don't want to - they are reasonably tight, but the system may process thousands per second.

Here's where I'm going with this: I can get around using a function that takes &Message by inlining that function into the parent function that creates the Messages (from a gRPC stream serving JSON). But that's ugly!

I do not want a language to dictate how I arrange & refactoring functional units. But I find myself feeling that Rust does just that.

Am I missing something?

3 posts - 2 participants

Read full topic

🏷️ rust_feed