Escape sequences in identifiers
C# and JavaScript allows escape sequences to be used in identifiers. Both languages use the same Unicode identifier convention as Rust, but also allows Unicode escape sequences to be inserted directly into identifiers. This is useful for calling foreign code with Unicode identifiers in them.
Edit: "Foreign" code includes Rust code imported from other modules or crates which may use Unicode characters in identifiers, not just code in other languages.
This is useful for calling foreign code with Unicode identifiers in them.
Note that you can already do this:
extern "C" {
#[link_name = "λö§"]
fn lambda_omlaut_para();
}
fn main() {
unsafe { lambda_omlaut_para(); }
}
On playground we get this linker error:
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" "/tmp/rustcrwTOxA/symbols.o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004.playground.8b8c9ccc-cgu.0.rcgu.o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004.4i1b8ushxzihkqu6.rcgu.o" "-Wl,--as-needed" "-L" "/playground/target/debug/deps" "-L" "/playground/target/debug/build/libsqlite3-sys-66f95ec5f0e221b1/out" "-L" "/playground/target/debug/build/ring-4aead75a5a56aa0f/out" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-7ca39ac42651c3df.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-62c6d032818141a1.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-50484fc03eb1eb5b.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-758be083b246d9c6.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-3cdf9a3c68f76e2d.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-3a1b74821c25a0e1.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-e046d82ebd84bb7f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-a61cdd33cfa8394f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-410c38f8df854235.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-f79d7458e122215f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-7d24b750ce5b22e8.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-4c2aa1ea3133ab73.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-41220dc85a7f114f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-230d004276c898f9.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-03ae30169a5438be.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-6f82c44b7818af35.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-1a0b7681f7efa789.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-83735dd4dae9b02c.rlib" "-Wl,--end-group" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-385029872275478f.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
= note: /usr/bin/ld: /playground/target/debug/deps/playground-f30fbb6d88a6f004.playground.8b8c9ccc-cgu.0.rcgu.o: in function `playground::main':
/playground/src/main.rs:7: undefined reference to `λö§'
collect2: error: ld returned 1 exit status
= help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
Also, as another aside, out of λö§, only § is not valid in Rust identifiers, and generally any language that uses Unicode-recommended XID_Start XID_Continue* identifiers will have matching restrictions to Rust.
I mean calling code in other Rust crates, not "foreign" code as in code in other languages. C# and JavaScript are cited only as languages that have the feature I am proposing. Since Rust supports Unicode identifiers, it should also support escape sequences to make them easy to type.
I encourage you to provide an example in this issue with what exactly you want to enable and why it would be useful.
I mean calling code in other Rust crates, not "foreign" code as in code in other languages.
Ah, my bad, "foreign" is almost always used in the context of "FFI" (Foreign Function Interface) in Rust, so I just assumed that's what you meant.
it should also support escape sequences to make them easy to type
I would expect that with both unicode (when you can't type them directly) and escape sequences, you'd almost always copy-paste such identifiers (instead of trying to inputs dozens of hex nibbles by hand).
cc @Manishearth
Yeah, I agree with @shepmaster here: I'd like to see an example.
Overall the point of identifiers is for them to be read by the programmer, I do not think people should define identifiers that need escapes to be distinguishable.
(Also, JS and C# do not use the same identifier conventions as Rust)
For example, сумма could be represented as \u{441}\u{443}\u{43C}\u{43C}\u{430} and 總和 could be represented as \u{7E3D}\u{548C}. Existing equivalent identifiers will continue to be equivalent, as in C# and JavaScript which also use XID_Start and XID_Continue. Current Rust already allows people to define visually identical identifiers. This allows IDEs to autocomplete the right identifier for you.
That's not a sufficient example, I understand that we allow non ascii identifiers (I designed the feature), but so far I don't see any strong motivation for why one would want to use escapes to refer to them.
Autocomplete ought to work with them already, and there's already builtin protections around potentially visually identical identifiers.
My keyboard and environment doesn't allow me to enter non-ASCII characters, is there any other way for me to refer to any identifier without having to type Unicode characters? Does Rust even allow entering the whole source code file in Base58 or Base64 or UTF-7? If you didn't support non-ASCII identifiers there would be no problem, now that you support them you must allow people to easily type them.
copy paste them, they're identifiers
is typing out an escape sequence really better?
in general I'd say the ability of their target users to type API calls is something library designers have to determine. If they're making a crate for use amongst the general rust community, it's probably not a good idea for them to use non ascii identifiers in public APIs. I'd kinda imagine this feature would be used more for local identifiers and public ones for multi crate applications, not library APIs.
Overall this feels like a solution in search of a problem: we don't even have many crates doing this yet.
I'm inclined to close this (in part because issues on the RFC repo are not really a part of the process)
Of course it is better, copy-pasting multiple identifiers is very slow and may not even be possible, compared to typing escape sequences and then tab. Should I start by writing a RFC and submit a pull-request? The instructions on this repo are very confusing, which is why I started on this "Issues" tab.
@kryptine When you said "type escape sequence and then tab", i think you're talking about IDE experience, instead of the interaction between actual stored-on-disk source code file and the compiler. If that's the case, rust-analyzer is the place you want to talk about this. If there's any more questions, feel free to create a topic on our Zulip!
You could write an RFC. It may help to write a pre-rfc first on internals.rust-lang.org. I am skeptical that it will be accepted, however (not that I am in charge of that discussion).
Here is an example where people use unfamiliar unicode codepoint to prevent others from using it:
https://docs.rs/lending-iterator/latest/lending_iterator/lending_iterator/trait.LendingIterator%E0%B6%9EItem.html
If you find it hard to type certain characters, it's a feature, not a bug.
Why would you have non-ASCII identifiers to start with if you did not intend to type them out directly?
Why would you have non-ASCII identifiers to start with if you did not intend to type them out directly?
The author of a library that uses non-ASCII identifiers may not be the same person as the consumer of the library. The author may have the capability to enter the non-ASCII identifiers while the consumer does not.
Note that I don't particularly want Rust to gain di/trigraph-equivalents, but it's worth understanding the original poster's intent.