# It is not strictly true that "items from traits can only be used if the trait is in scope"

**URL:** <https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242>\
**Category:** documentation\
**Created:** [July 24, 2024, 5:55pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242 "2024-07-24T17:55:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 24, 2024, 5:55pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/1 "2024-07-24T17:55:20Z")

</div>

take this simple program.

```rust
use std::io;

fn main() {
    <io::Stdout as io::Write>::write(&mut io::stdout(), b"hello").unwrap();
}

```

this form is actually useful in niche situations, such as if you already have a different `Write` trait in scope.

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [July 24, 2024, 6:28pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/2 "2024-07-24T18:28:22Z")

</div>

I think even just this will work

```rust
use std::io;

fn main() {
    io::Write::write(&mut io::stdout(), b"hello").unwrap();
}

```

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 24, 2024, 6:32pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/3 "2024-07-24T18:32:53Z")

</div>

> [@pitaj](#):
>
> ```rust
> use std::io;
> 
> fn main() {
> io::Write::write(&mut io::stdout(), b"hello").unwrap();
> }
> 
> ```

indeed it does, the longer form isn't required for items callable as methods since rust can look at the type of the `self` argument to know what definition to dispatch on.

---

<div class="post-metadata">

**Author:** ![jvcmarcenes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jvcmarcenes/32/11971_2.png) [@jvcmarcenes](https://internals.rust-lang.org/u/jvcmarcenes)\
**Post date:** [July 25, 2024, 3:10pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/4 "2024-07-25T15:10:36Z")

</div>

`io::Write` this expression brings the trait into the scope for the expression.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 25, 2024, 9:41pm UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/5 "2024-07-25T21:41:36Z")

</div>

it does not. `io::Write::write(&mut io::stdout(), { io::stdout().write(b"foo"); b"bar" })` will fail to compile due to `Write` not being in scope.

---

<div class="post-metadata">

**Author:** ![jvcmarcenes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jvcmarcenes/32/11971_2.png) [@jvcmarcenes](https://internals.rust-lang.org/u/jvcmarcenes)\
**Post date:** [July 26, 2024, 12:48am UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/6 "2024-07-26T00:48:09Z")

</div>

I meant more in the expression that evaluates to which function to call, not inside the whole expression. to me the usage OP showed still counts as putting the trait _in-scope_

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 24, 2024, 12:48am UTC](https://internals.rust-lang.org/t/it-is-not-strictly-true-that-items-from-traits-can-only-be-used-if-the-trait-is-in-scope/21242/7 "2024-10-24T00:48:45Z")

</div>

Closed on October 24, 2024, 12:48am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
