I do not find the term item satisfying. It’s somewhat meaningless and is not even explained in the reference. What are these things called in other languages? Surely there must be something better out there?
Items are the “top level” of a Rust crate if name resolution is ignored. When compared to other languages, Rust has a relatively rich set of top-level things. I think C-family languages call these “declarations”.
There’s also the “are impl-items and trait-items items?” business
No, in C-like languages “items” would be either declarations or definitions, except that I’m not sure use
statements (or using namespace
) fit that classification (well, the reference calls use
statements declarations). I’m not aware of a general classification more specific than “item” (except maybe “line”).
Maybe simply replacing “items” with “declarations and definitions” is good enough?
Rust does not really have “declarations” in the C sense (except for externs, and these are used only for FFI), only definitions (I mixed these 2 up). use
statements are not items, in any case.
use
statements are “items” according to the linked reference. I think the idea is that the crate can be chopped up into a series of items.
Rust does have “declarations”: mod foo;
is exactly a declaration according to the C sense. (You could, I suppose, argue that this is not a crate-level construct since from the point of view of compiling the crate (in the naive view at least) mod foo;
is replaced with the entire module: mod foo { ... }
, neverless mod
declarations do occur at the file level.)
As for extern crate
and use ...
, they are either declarations or instructions (or some word to that effect).
Then there are attributes and macro definitions, neither of which are listed in that section of the reference.
use
and extern crate
are not really items (at least from an implementation standpoint), regardless of what the reference says - they merely reference other items. I think “directive” may be the word we want.
attributes and macro definitions are also not items - attributes are attached to something (which may be a proper outer item or some inner item - e.g. a struct field), while macro definitions, along with mod foo;
, are defined solely by their expansion.
Ah, I was thinking in terms of the contents of the file. You’re correct, those things are not really “items” from the point of view of the crate (the use
and extern crate
directives are more like attributes of the module and crate respectively).
Note that traits do use declarations in the C sense.
At the crate level though, are we agreed that “items” are either directives or definitions?
Traits are definitions in the same way C structs definitions are. Anyway, from an implementation standpoint, we only sometimes regard directives as items.
Not traits themselves, but the functions/types/constants declared within.
Don’t fix what ain’t broke. ‘Item’ is a perfectly descriptive name, given that the term is so generic.
Saying item is okay is like saying thing would also be okay. It’s too generic.
“item” is a rather generic concept (especially if we also count impl-items and trait-items), and most places deal which more specific items (e.g. “struct”, “function”).
‘thing’ isn’t okay not because it’s too generic, but because it’s too casual. ‘item’ is okay because that’s all they are: items. There’s nothing else tying them together. The only alternative is a term like ‘declarations and definitions’ which isn’t a term, it’s a list of terms. You can call them ‘structs, functions, …’ but again that is just a list of things, it isn’t a name for that grouping as a whole.
Anything can be called an item, but in the case of Rust, an item is something specific. That’s why I think using item is not ideal. top-levels would be a better name, but there are likely better names.
There are lots of things that can be called “type” (or “value”), but Rust gives these terms a specific meaning. We could use better names (for item, outer-item, impl-item, trait-item, variant field). I prefer “item” rather than “definition” because cross-crate items are not really definitions.
Except that items don’t have to appear at the “top-level” you can define structs and functions inside a function body.
Item is fine in context and it’s not really a term that you need to know, it’s mostly an implementation and reference concern.
I did see it used in error messages, and that wasn’t helpful at the time since I didn’t know it had its own special Rust meaning.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.