Static analysis/highlighting of pure functions in rust

Hi All,
One of my favorite features in rust/rust rust analyser is the really strong control over where/when variables are mutable and the fact that rust analyser underlines mutable variables so you can tell immediately if you are dealing with a mutable or immutable variable.

Considering rust has excellent support for functional programming in the language and community I was wondering if there is anything similar for functions so you could tell at a glance if you are dealing with a pure or impure function. Ideally this would also apply to methods (as due to rust not having inheritance methods are pretty much just syntax sugar for passing a struct into a function).

Definition:
For the purposes of this discussion by a "pure" function I mean one which does not have side effects and where output is determined solely by input. Though if there's something which uses a slightly different definition that's grand too.

Thanks

Clarification:
To clarify from SkiFire13's comment about conditionally pure functions.

The issue I see with this is how to handle functions that are conditionally pure. For example is Option::map pure? It might not, if the closure passed to it is not pure.

What I am looking for is a tool which can evaluate that so a call containing only pure functions will show up as pure but one containing impure functions will not. FP is great but sometimes you need/want impure code for certain applications. I want to know which parts of my code are functional and which aren't

It's not the exact same thing, but clippy complaining that a function could be const is usually a good enough indicator to me that a certain function is pure.

1 Like

The issue I see with this is how to handle functions that are conditionally pure. For example is Option::map pure? It might not, if the closure passed to it is not pure.

1 Like

Well ideally the tool would evaluate the combination so anything containing a call to an impure function would show up as impure and anything containing only pure functions would show up as pure.

Essentially I want to be able to easily see which parts of my code are pure and which aren't.

Surely you don’t mean that, right? Why do you require a “pure” function to be injective? You don’t think absolute value of signed integers is “pure” because for every non-zero output there are two inputs? Do you mean “total” instead of injective?

Ah, never mind. I misinterpreted the sentence. You’re saying a given input always maps to the same output (i.e., a partial function in the math sense).

Sorry I that should have been purely not uniquely. I.e. you always get the same output for a given input.