|
1 | 1 |
|
2 |
| -# rust-script [](https://github.com/fornwall/rust-script/actions?query=workflow%3ACI) [](https://crates.io/crates/rust-script) |
| 2 | +[](https://github.com/fornwall/rust-script/actions?query=workflow%3ACI) |
| 3 | +[](https://crates.io/crates/rust-script) |
3 | 4 |
|
4 |
| -`rust-script` run Rust files and expressions without any setup or compilation necessary. |
5 |
| - |
6 |
| -Some of `rust-script`'s features include: |
7 |
| - |
8 |
| -- Reading Cargo manifests embedded in Rust scripts. |
9 |
| -- Caching compiled artifacts (including dependencies) to amortise build times. |
10 |
| -- Supporting executable Rust scripts via UNIX shebangs and Windows file associations. |
11 |
| -- Evaluating expressions on the command-line. |
12 |
| -- Using expressions as stream filters (*i.e.* for use in command pipelines). |
13 |
| -- Running unit tests and benchmarks from scripts. |
14 |
| -- Custom templates for command-line expressions and filters. |
15 |
| - |
16 |
| -Table of contents: |
17 |
| - |
18 |
| -- [Installation](#installation) |
19 |
| - - [Self-Executing Scripts](#shebang) |
20 |
| -- [Usage](#usage) |
21 |
| - - [Scripts](#scripts) |
22 |
| - - [Expressions](#expressions) |
23 |
| - - [Stream Filters](#filters) |
24 |
| - - [Environment Variables](#env-vars) |
25 |
| - - [Templates](#templates) |
26 |
| - - [Troubleshooting](#troubleshooting) |
27 |
| -- [License](#license) |
28 |
| - |
29 |
| -<a name="installation"></a> |
30 |
| -## Installation |
31 |
| - |
32 |
| -Install or update `rust-script` using Cargo: |
33 |
| - |
34 |
| -```sh |
35 |
| -cargo install --force rust-script |
36 |
| -``` |
37 |
| - |
38 |
| -Currently the latest stable version of Rust (1.47.0) is required. |
39 |
| - |
40 |
| -<a name="shebang"></a> |
41 |
| -### Self-Executing Scripts |
42 |
| - |
43 |
| -On Unix systems, you can use `#!/usr/bin/env rust-script` as a shebang line in a Rust script. This will allow you to execute a script file directly. |
44 |
| - |
45 |
| -If you are using Windows, you can associate the `.crs` extension (which is simply a renamed `.rs` file) with `rust-script`. This allows you to execute Rust scripts simply by naming them like any other executable or script. |
46 |
| - |
47 |
| -This can be done using the `rust-script --install-file-association` command. It will also allow you to execute `.crs` scripts *without* having to specify the file extension, in the same way that `.exe` and `.bat` files can be used. |
48 |
| - |
49 |
| -Uninstall the file association with `rust-script --uninstall-file-association`. |
50 |
| - |
51 |
| -If you want to make a script usable across platforms, use *both* a hashbang line *and* give the file a `.crs` file extension. |
52 |
| - |
53 |
| -<a name="usage"></a> |
54 |
| -## Usage |
55 |
| - |
56 |
| -Generally, you can get an overview of the available options using the `--help` flag. |
57 |
| - |
58 |
| -<a name="scripts"></a> |
59 |
| -### Scripts |
60 |
| - |
61 |
| -The primary use for `rust-script` is for running Rust source files as scripts. For example: |
62 |
| - |
63 |
| -```shell |
64 |
| -$ echo 'fn main() { println!("Hello, World!"); }' > hello.rs |
65 |
| -$ rust-script hello.rs |
66 |
| -Hello, World! |
67 |
| -$ rust-script hello # you can leave off the file extension |
68 |
| -Hello, World! |
69 |
| -``` |
70 |
| - |
71 |
| -The output of Cargo will be hidden unless compilation fails, or takes longer than a few seconds. |
72 |
| - |
73 |
| -`rust-script` will also look for embedded dependency and manifest information in the script. For example, all of the following are equivalent: |
74 |
| - |
75 |
| -- `now.crs` (code block manifest with UNIX hashbang and `.crs` extension): |
76 |
| - |
77 |
| - ```rust |
78 |
| - #!/usr/bin/env rust-script |
79 |
| - //! This is a regular crate doc comment, but it also contains a partial |
80 |
| - //! Cargo manifest. Note the use of a *fenced* code block, and the |
81 |
| - //! `cargo` "language". |
82 |
| - //! |
83 |
| - //! ```cargo |
84 |
| - //! [dependencies] |
85 |
| - //! time = "0.1.25" |
86 |
| - //! ``` |
87 |
| - extern crate time; |
88 |
| - fn main() { |
89 |
| - println!("{}", time::now().rfc822z()); |
90 |
| - } |
91 |
| - ``` |
92 |
| - |
93 |
| -- `now.rs` (dependency-only, short-hand manifest): |
94 |
| - |
95 |
| - ```rust |
96 |
| - // cargo-deps: time="0.1.25" |
97 |
| - // You can also leave off the version number, in which case, it's assumed |
98 |
| - // to be "*". Also, the `cargo-deps` comment *must* be a single-line |
99 |
| - // comment, and it *must* be the first thing in the file, after the |
100 |
| - // hashbang. |
101 |
| - extern crate time; |
102 |
| - fn main() { |
103 |
| - println!("{}", time::now().rfc822z()); |
104 |
| - } |
105 |
| - ``` |
106 |
| - |
107 |
| - > **Note**: you can write multiple dependencies by separating them with commas. *E.g.* `time="0.1.25", libc="0.2.5"`. |
108 |
| - |
109 |
| -On running either of these, `rust-script` will generate a Cargo package, build it, and run the result. The output may look something like: |
110 |
| - |
111 |
| -```shell |
112 |
| -$ rust-script now |
113 |
| - Updating registry `https://github.com/rust-lang/crates.io-index` |
114 |
| - Compiling winapi-build v0.1.1 |
115 |
| - Compiling winapi v0.2.8 |
116 |
| - Compiling libc v0.2.30 |
117 |
| - Compiling kernel32-sys v0.2.2 |
118 |
| - Compiling time v0.1.38 |
119 |
| - Compiling now v0.1.0 (file:///C:/Users/drk/AppData/Local/Cargo/script-cache/file-now-37cb982cd51cc8b1) |
120 |
| - Finished release [optimized] target(s) in 49.7 secs |
121 |
| -Sun, 17 Sep 2017 20:38:58 +1000 |
122 |
| -``` |
123 |
| - |
124 |
| -Subsequent runs, provided the script has not changed, will likely just run the cached executable directly: |
125 |
| - |
126 |
| -```shell |
127 |
| -$ rust-script now |
128 |
| -Sun, 17 Sep 2017 20:39:40 +1000 |
129 |
| -``` |
| 5 | +# rust-script |
130 | 6 |
|
131 |
| -Useful command-line arguments: |
132 |
| - |
133 |
| -- `--bench`: Compile and run benchmarks. Requires a nightly toolchain. |
134 |
| -- `--debug`: Build a debug executable, not an optimised one. |
135 |
| -- `--features <features>`: Cargo features to pass when building and running. |
136 |
| -- `--force`: Force the script to be rebuilt. Useful if you want to force a recompile with a different toolchain. |
137 |
| -- `--gen-pkg-only`: Generate the Cargo package, but don't compile or run it. Effectively "unpacks" the script into a Cargo package. |
138 |
| -- `--test`: Compile and run tests. |
139 |
| - |
140 |
| -<a name="expressions"></a> |
141 |
| -### Expressions |
142 |
| - |
143 |
| -`rust-script` can also run pieces of Rust code directly from the command line. This is done by providing the `--expr` option; this causes `rust-script` to interpret the `<script>` argument as source code *instead* of as a file path. For example, code can be executed from the command line in a number of ways: |
144 |
| - |
145 |
| -- `rust-script --dep time --expr "extern crate time; time::OffsetDateTime::now_utc().format(time::Format::Rfc3339).to_string()"` |
146 |
| -- `rust-script --dep time=0.1.38 --expr "extern crate time; time::now().rfc822z().to_string()"` - uses a specific version of `time` |
147 |
| -- `rust-script -D time -e "..."` - guess and inject `extern crate time`; this only works when the package and crate names of a dependency match. |
148 |
| -- `rust-script -d time -x time -e "..."` - injects `extern crate time`; works when the names do *not* match. |
149 |
| - |
150 |
| -The code given is embedded into a block expression, evaluated, and printed out using the `Debug` formatter (*i.e.* `{:?}`). |
151 |
| - |
152 |
| -Useful command-line arguments: |
153 |
| - |
154 |
| -- `-d`/`--dep`: add a dependency to the generated `Cargo.toml` manifest. |
155 |
| -- `-x`/`--extern`: inject `extern crate` into generated script. |
156 |
| -- `-D`/`--dep-extern`: do both of the above. |
157 |
| -- `-t`/`--template`: Specify a custom template for this expression (see section on templates). |
158 |
| - |
159 |
| -<a name="filters"></a> |
160 |
| -### Stream Filters |
161 |
| - |
162 |
| -You can use `rust-script` to write a quick stream filter, by specifying a closure to be called for each line read from stdin, like so: |
163 |
| - |
164 |
| -```text |
165 |
| -$ cat now.crs | rust-script --loop \ |
166 |
| - "let mut n=0; move |l| {n+=1; println!(\"{:>6}: {}\",n,l.trim_right())}" |
167 |
| - Compiling loop v0.1.0 (file:///C:/Users/drk/AppData/Local/Cargo/script-cache/loop-58079283761aab8433b1) |
168 |
| - 1: // cargo-deps: time="0.1.25" |
169 |
| - 2: extern crate time; |
170 |
| - 3: fn main() { |
171 |
| - 4: println!("{}", time::now().rfc822z()); |
172 |
| - 5: } |
173 |
| -``` |
174 |
| - |
175 |
| -You can achieve a similar effect to the above by using the `--count` flag, which causes the line number to be passed as a second argument to your closure: |
176 |
| - |
177 |
| -```text |
178 |
| -$ cat now.crs | rust-script --count --loop \ |
179 |
| - "|l,n| println!(\"{:>6}: {}\", n, l.trim_right())" |
180 |
| - Compiling loop v0.1.0 (file:///C:/Users/drk/AppData/Local/Cargo/script-cache/loop-58079283761aab8433b1) |
181 |
| - 1: // cargo-deps: time="0.1.25" |
182 |
| - 2: extern crate time; |
183 |
| - 3: fn main() { |
184 |
| - 4: println!("{}", time::now().rfc822z()); |
185 |
| - 5: } |
186 |
| -``` |
187 |
| - |
188 |
| -Note that, like with expressions, you can specify a custom template for stream filters. |
189 |
| - |
190 |
| -<a name="env-vars"></a> |
191 |
| -### Environment Variables |
192 |
| - |
193 |
| -The following environment variables are provided to scripts by `rust-script`: |
194 |
| - |
195 |
| -- `CARGO_SCRIPT_BASE_PATH`: the base path used by `rust-script` to resolve relative dependency paths. Note that this is *not* necessarily the same as either the working directory, or the directory in which the script is being compiled. |
196 |
| - |
197 |
| -- `CARGO_SCRIPT_PKG_NAME`: the generated package name of the script. |
198 |
| - |
199 |
| -- `CARGO_SCRIPT_SAFE_NAME`: the file name of the script (sans file extension) being run. For scripts, this is derived from the script's filename. May also be `"expr"` or `"loop"` for those invocations. |
200 |
| - |
201 |
| -- `CARGO_SCRIPT_SCRIPT_PATH`: absolute path to the script being run, assuming one exists. Set to the empty string for expressions. |
202 |
| - |
203 |
| -<a name="templates"></a> |
204 |
| -### Templates |
205 |
| - |
206 |
| -You can use templates to avoid having to re-specify common code and dependencies. You can view a list of your templates by running `rust-script templates list` (note the hyphen), or show the folder in which they should be stored by running `rust-script templates show`. You can dump the contents of a template using `rust-script templates dump NAME`. |
207 |
| - |
208 |
| -Templates are Rust source files with two placeholders: `#{prelude}` for the auto-generated prelude (which should be placed at the top of the template), and `#{script}` for the contents of the script itself. |
209 |
| - |
210 |
| -For example, a minimal expression template that adds a dependency and imports some additional symbols might be: |
211 |
| - |
212 |
| -```rust |
213 |
| -// cargo-deps: itertools="0.6.2" |
214 |
| -#![allow(unused_imports)] |
215 |
| -#{prelude} |
216 |
| -extern crate itertools; |
217 |
| -use std::io::prelude::*; |
218 |
| -use std::mem; |
219 |
| -use itertools::Itertools; |
220 |
| - |
221 |
| -fn main() { |
222 |
| - let result = { |
223 |
| - #{script} |
224 |
| - }; |
225 |
| - println!("{:?}", result); |
226 |
| -} |
227 |
| -``` |
228 |
| - |
229 |
| -If stored in the templates folder as `grabbag.rs`, you can use it by passing the name `grabbag` via the `--template` option, like so: |
230 |
| - |
231 |
| -```text |
232 |
| -$ rust-script -t grabbag -e "mem::size_of::<Box<Read>>()" |
233 |
| -16 |
234 |
| -``` |
235 |
| - |
236 |
| -In addition, there are three built-in templates: `expr`, `loop`, and `loop-count`. These are used for the `--expr`, `--loop`, and `--loop --count` invocation forms. They can be overridden by placing templates with the same name in the template folder. If you have *not* overridden them, you can dump the contents of these built-in templates using the `templates dump` command noted above. |
237 |
| - |
238 |
| -<a name="troubleshooting"></a> |
239 |
| -### Troubleshooting |
| 7 | +`rust-script` run Rust files and expressions without any setup or compilation necessary. |
240 | 8 |
|
241 |
| -Run with the `RUST_LOG=rust_script=trace` environment variable set to see debug log output. |
| 9 | +See the documentation at [rust-script.org](https://rust-script.org). |
242 | 10 |
|
243 |
| -<a name="license"></a> |
244 | 11 | ## License
|
245 | 12 |
|
246 |
| -`rust-script` is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) |
247 |
| -and the [Apache License (Version 2.0)](LICENSE-APACHE) |
248 |
| -). |
| 13 | +`rust-script` is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE). |
0 commit comments