Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get type hints to display?

I have seen youtubers and such working on Rust in VSC with rust-analyzer plug-in where they get the optional type annotations displayed, even if it isn't necessarily written in the code. It's like I type foo(a,b) in the editor and it automagically displays foo(a: A, b: B) where the :A and :B are in faint grey possibly not even written in the file, just visual hint? It's nice and I can't figure out whether this is a feature of VSC or rust-analyzer? My rust-analyzer has the two settings Parameter Hints and TypeHints both set to enabled.

like image 781
sphere Avatar asked Feb 12 '21 15:02

sphere


People also ask

How do you get hints in Python?

Python's type hints provide you with optional static typing to leverage the best of both static and dynamic typing. Besides the str type, you can use other built-in types such as int , float , bool , and bytes for type hintings. To check the syntax for type hints, you need to use a static type checker tool.

How do I get hints for Visual Studio code?

Ctrl + Space shows hints when used while cursor is placed over the method name, but not within the parentheses, what is what the op asked for. Ctrl + Shift + Space is what he is looking for.

Does Python enforce type hints?

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar , and Generic .

Should I use Python type hints?

In his excellent article The State of Type Hints in Python, Bernát Gábor recommends that “type hints should be used whenever unit tests are worth writing.” Indeed, type hints play a similar role as tests in your code: they help you as a developer write better code.


1 Answers

You're looking for parameter hints in this case. The function for which you want to display hints also needs to have more than one parameter.

Make sure that the setting is enabled:

Settings (UI)

Inlay hints: Parameter hints

Settings (JSON)

"rust-analyzer.inlayHints.parameterHints": true

You should then end up with something akin to the following:

fn add(x: u32, y: u32) -> u32 {
    x + y    
}

fn main() {
    let result = add(x: 4, y: 2);
}

Make sure that only rust-analyzer is enabled as it can conflict with rls. A warning was added which mentions the following if both are enabled:

You have both rust-analyzer (matklad.rust-analyzer) and Rust (rust-lang.rust)
plugins enabled. These are known to conflict and cause various functions of
both plugins to not work correctly. You should disable one of them.
like image 123
Jason Avatar answered Oct 05 '22 14:10

Jason