What is the conventon to name a move fn without using "move"

move is a dedicated keyword in Rust, so it cannot be used as a function name.
Is there a convention on what scheme/idiom to use? I've used mve but i don't really like it?

Name the function after what it does. Usually, passing by value is the default in Rust, so appending move to a function name is nothing but noise.

well I have a motor, and i impl a method to make it move, so... yeah, move is quite descriptive

3 Likes

I would use terms that suggest the specific kind of motion or change of motion the method is intended to produce, rather than "move." If the situation is as simple as either turning at full speed or stopping, then "rotate()" or even "start()" might be appropriate; if there's more control, then "accelerate()" or "rotate(rpm)" or similar may be more suitable.

You can always use r#move, as Rust does allow you to use keywords as symbols if you escape them, but the ergonomics aren't great and I'd avoid it if you aren't in a situation where you absolutely must use that name.

2 Likes

I guess that's a good workflow

I wanted to use the keyword match as a variable name and went with match_ (suffixed underscore).

To the extent of my knowledge, the only keywords with "clever" somewhat conventional alternative spellings are:

keyword identifier
as az
crate krate
self this
Self This
type ty
macro mac
unsized unsize
try tri

(Though to be clear: I very much could be hallucinating some of these and/or missing some.)

For any other keyword (and honestly, even for these), either find a reasonable synonym for the domain (e.g. drive), add prepositions (e.g. move_to), or just give up and use an underscore (i.e. move_) or raw identifier (r#move) if you can't come up with any other option.

This was more commonly written as just ty, AFAIK.

You're right, of course. It's late and I guess I made something up, knowing there was a typical short form. :person_shrugging:

Technically it can be used, of course. Just write it like r#move.

Whether it's good idea to use it depends on the situation. I would only use it like that if name is used in FFI or if it's fixed by some kind of external standard.

I've run into this in the past and I'll normally modify the name to include the type of movement. For example, linear_move() or circular_move().

Here's a list from rust-analyzer (docs/dev/style.md):

crate  -> krate
enum   -> enum_
fn     -> func
impl   -> imp
macro  -> mac
mod    -> module
struct -> strukt
trait  -> trait_
type   -> ty
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.