rust-lang / rust-lang/rfcs

Define crates version in rust files

Open
#2,905 13 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

it would be handy if you could use crates just from an rs file by defining its version in it, something like

extern crate rand = "0.7.3";

or

extern crate rand = "*";

or maybe even define "*" as default if no version is provided.
using the as keyword if the version is specified would allow to do :

extern crate rand = "0.7.3" as randomexample;

Though it shouldn't be the default and defining them in cargo should remain the prefered way.

Why should we add this feature ?

So let's say i want to use rust as a single file script but want to use crates in that "script"
it would be handy to have rustc or another tool be able to pull crates, but in order to do that it'd need to be version aware, thus being able to define it in .rs files would be required.

that would also allow to use a shebang in the file that could look like this

#!rustrunner
//rest of the file

where rustrunner is an hypotethical command that would read the file, find the used crates, pull them in some shared directory, compile the file (maybe in /tmp) and run it.
unless it was a jit, anyway, same idea.

using something like that rustrunner would allow us to do that without needing to edit rustc to pull crates whilst still allowing to define crate version in rust files so another tool could use it.

And lastly, a side thought, idk if it is already the case (i don't think so), but it would be handy if cargo stored its compiled crates in ~/.cargo, it would allow it to not recompile deps in another project

which would come hand in hand with that new feature since "rustrunner" wouldn't need to recompile all the crates each time the "script" is runned, it could just pull them from ~/.cargo.

PS: hope the english wasn't too bad, i'm non native, eager to ear your thoughts.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed extern crate version syntax and how rustc and Cargo currently handle dependencies. Consider the interaction with the suggested rustrunner workflow and shared ~/.cargo storage. Done would require an agreed feature design and resolution of the syntax, dependency-fetching, and caching questions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.