What is the meaning of `use path::{self}`?
Asked Answered
N

2

6

I recently read the following line of code:

use fmt::{self, Debug}; 

What does the self keyword in the above statement mean?

Neurotic answered 8/3, 2018 at 23:14 Comment(0)
U
6

Using self in that context allows you to bind a module plus some of it's child elements into the current scope with a single use statement.

Without self:

use a::b::{c,d};
// Now you can refer to a::b::c as c and a::b::d as d
// but if you need to refer to a::b as a::b

With self:

use a::b::{self, c, d};
// Now b refers to a::b as well
Unprincipled answered 8/3, 2018 at 23:26 Comment(11)
could you point why i would need to use both in case of fmt (why it's not enough to write just fmt::Debug).?Neurotic
fmt contains the struct Result. Personally I don't want to overwrite Result in my namespace, so I use fmt::Result. I.e. fn fmt(&self, f: Formatter) -> fmt::Result;. You only need this, if you want to use the module itself.Confront
@Tim, what role does 'self' play in example provided in the question.? from my understaning, after removing 'self' I will still be able to use 'fmt', since it's introduced erlier into the root scope.Neurotic
@GreenTree no, you won't be able to use fmt, if you remove selfConfront
@Tim ideone.com/n4tXWB but why such 'direct' usage in 'goo' allowed, right after mod declaration, without no 'use' statement.?Neurotic
@GreenTree You declared the mod boo just before. Why accepting an answer if you don't understand it?Confront
@GreenTree use the Rust Playground instead of ideone.com for rust.Confront
@Tim, because it does answer the question raised. i'm trying to clarify what i'm missing in the example. if use allows only absolute paths, 'fmt' should be start of the path, module, right.? so it should be at least declared with mod fmt erlier - if so, use fmt::{self} is redeclaration.Neurotic
@GreenTree not necessarily earlier, but anywhere, yes.Confront
@Tim, so why the build does not fail with error (fmt reimported here).?Neurotic
@GreenTree it does not. The mod is defined somewhere else.Confront
E
8

self here refers to the module itself, i.e. your line is equivalent to the two lines

use fmt::Debug;
use fmt;
Ether answered 8/3, 2018 at 23:18 Comment(0)
U
6

Using self in that context allows you to bind a module plus some of it's child elements into the current scope with a single use statement.

Without self:

use a::b::{c,d};
// Now you can refer to a::b::c as c and a::b::d as d
// but if you need to refer to a::b as a::b

With self:

use a::b::{self, c, d};
// Now b refers to a::b as well
Unprincipled answered 8/3, 2018 at 23:26 Comment(11)
could you point why i would need to use both in case of fmt (why it's not enough to write just fmt::Debug).?Neurotic
fmt contains the struct Result. Personally I don't want to overwrite Result in my namespace, so I use fmt::Result. I.e. fn fmt(&self, f: Formatter) -> fmt::Result;. You only need this, if you want to use the module itself.Confront
@Tim, what role does 'self' play in example provided in the question.? from my understaning, after removing 'self' I will still be able to use 'fmt', since it's introduced erlier into the root scope.Neurotic
@GreenTree no, you won't be able to use fmt, if you remove selfConfront
@Tim ideone.com/n4tXWB but why such 'direct' usage in 'goo' allowed, right after mod declaration, without no 'use' statement.?Neurotic
@GreenTree You declared the mod boo just before. Why accepting an answer if you don't understand it?Confront
@GreenTree use the Rust Playground instead of ideone.com for rust.Confront
@Tim, because it does answer the question raised. i'm trying to clarify what i'm missing in the example. if use allows only absolute paths, 'fmt' should be start of the path, module, right.? so it should be at least declared with mod fmt erlier - if so, use fmt::{self} is redeclaration.Neurotic
@GreenTree not necessarily earlier, but anywhere, yes.Confront
@Tim, so why the build does not fail with error (fmt reimported here).?Neurotic
@GreenTree it does not. The mod is defined somewhere else.Confront

© 2022 - 2024 — McMap. All rights reserved.