# Aliasing other things

**URL:** <https://internals.rust-lang.org/t/aliasing-other-things/12288>\
**Category:** language design\
**Created:** [May 6, 2020, 10:52pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288 "2020-05-06T22:52:56Z")\
**Posts on this page:** 5\
**Page:** 3

<div class="post-metadata">

**Author:** ![ogoffart](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ogoffart/32/12493_2.png) [@ogoffart](https://internals.rust-lang.org/u/ogoffart)\
**Post date:** [May 15, 2020, 3:04pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288/50 "2020-05-15T15:04:38Z")

</div>

I can see some value in aliasing names. In fact, rust already allow some form of aliasing using the `use` keyword. And probably we could extend it to new scopes.

```rust
struct Color;

// this already works today
use Color as Colour;

trait Hello {
   fn color(&self) -> Color;

  // proposed: allow to use `use` within `trait` and `impl`
  // of course, this would only allow to import item from the trait itself. 
  // (What would it means otherwise?)
   use color as colour;
}

// For enum and struct it is a bit more complicated since one cannot really put a `use`,
// but one could put them in a impl
struct Foo {
    color: usize,
}
impl Foo {
   use color as colour;
}

```

The color/colour example might not appear so usefull, but I see some value when one wants to deprecate names.

```rust
impl str {
   // new name
   pub fn trim_start(&self) -> &str { /* ... */ }

   // old deprecated name still available
   #[deprecated(since = "1.33.0", reason = "superseded by `trim_start`"]
   pub use trim_left = trim_start;
}

```

This syntax would allow to rename and keep the deprecate names of trait items or enum variant which may be desirable sometimes.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [May 15, 2020, 6:46pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288/51 "2020-05-15T18:46:50Z")

</div>

> [@ogoffart](#):
>
> (What would it means otherwise?)

`use` in an `impl` block could mean the exact same as `use` in the parent scope, just scoped to the impl block rather than the parent scope.

This does not prevent being used to alias methods, it just requires marking them `pub`.

It could also be used for delegation.

---

<div class="post-metadata">

**Author:** ![lordan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lordan/32/2107_2.png) [@lordan](https://internals.rust-lang.org/u/lordan)\
**Post date:** [May 15, 2020, 6:59pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288/52 "2020-05-15T18:59:18Z")

</div>

Deprecating and refactoring stuff is one of the few use cases where I think something like name aliases could be useful.

However, simply renaming stuff is rather limited, often you have logic changes that go with it. Hence why I think properties would be more useful (trait methods not withstanding).

Conversely, for accessibility I don't think this is the right approach at all: it would require _every_ crate author to annotate _everything_ ahead of time, in _all_ the relevant spellings, and yet avoid name clashes. This seems hardly feasible.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [May 15, 2020, 10:59pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288/53 "2020-05-15T22:59:59Z")

</div>

> [@lordan](#):
>
> However, simply renaming stuff is rather limited, often you have logic changes that go with it. Hence why I think properties would be more useful (trait methods not withstanding).

[I proposed properties before](https://internals.rust-lang.org/t/idea-properties/11152), but was ultimately convinced that they wouldn't be very useful in Rust, because struct fields can't be replaced with properties in a backwards-compatible way.

All proposals in this thread are flawed, since they doesn't work across crates. A better syntax would be

```rust
use some_crate::Foo::{self, self.color as colour};

let foo: Foo = ..;
assert_eq!(foo.color, foo.colour);

```

Note that `self.color` can be a field or a method. If a field and a method both have the name `color`, both are renamed.

This would even enable us to convert a tuple struct to a struct with named fields:

```rust
// this works today:
struct Foo(u8, u8, u8);
let _ = Foo { 0: 255, 1: 0, 2: 0 };

// if we could rename fields, we could write this:
use Foo.{0 as red, 1 as green, 2 as blue};
let _ = Foo { red: 255, green: 0, blue: 0 };

```

However, I'm still not convinced that this actually needed. Language features need a strong motivation; for example, type aliases are used to make type declarations smaller and less generic, and imported items can be renamed mainly to prevent _name clashes_ when importing multiple items of the same name.

---

<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:** [August 13, 2020, 11:00pm UTC](https://internals.rust-lang.org/t/aliasing-other-things/12288/54 "2020-08-13T23:00:12Z")

</div>

Closed on August 13, 2020, 11:00pm UTC

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

[Previous page](https://internals.rust-lang.org/t/aliasing-other-things/12288.md?page=2)
