1
1
# rust-script-ext
2
2
Opinionated set of extensions for use with
3
- [ ` rust-script ` ] ( https://github.com/fornwall/rust-script ) .
3
+ [ ` rust-script ` ] ( https://github.com/fornwall/rust-script ) or
4
+ [ ` cargo script ` ] ( https://github.com/rust-lang/rfcs/pull/3503 ) .
4
5
5
6
Using ` rust-script ` to run Rust like a shell script is great!
6
7
This crate provides an opinionated set of extensions tailored towards common patterns in scripts.
7
8
These patterns include file reading, argument parsing, error handling.
8
9
The goal is for script writers to focus on the _ business logic_ , not implementing parsers, handling
9
10
errors, parsing arguments, etc.
10
11
12
+ ## Using ` rust-script `
11
13
```` sh
12
14
$ cargo install rust-script
13
15
..
14
- $ cat ./template.rs
16
+ $ cat ./template-rust-script .rs
15
17
#! /usr/bin/env -S rust-script -c
16
18
//! You might need to chmod +x your script!
17
19
//! ` ` ` cargo
18
20
//! [dependencies.rust-script-ext]
19
21
//! git = " https://github.com/kurtlawrence/rust-script-ext"
20
22
//! rev = " e914bc0a04e9216ffdfd15c47f4c39b74d98bbeb"
21
23
//! ` ` `
22
-
24
+ // See < https://kurtlawrence.github.io/rust-script-ext/rust_script_ext/ > for documentation
23
25
use rust_script_ext::prelude::* ;
24
26
25
- fn main () {
27
+ fn main () - > Result< () > {
26
28
// fastrand comes from rust_script_ext::prelude::*
27
29
let n = std::iter::repeat_with(|| fastrand::u32(1..=100))
28
30
.take(5)
29
31
.collect::<Vec<_>> ();
30
32
31
33
println! (" Here's 5 random numbers: {n:?}" );
34
+ Ok( ())
32
35
}
33
- $ ./template.rs
36
+ $ ./template-rust-script .rs
34
37
Here' s 5 random numbers: [28, 97, 9, 23, 58]
35
38
````
36
39
40
+ ## Using `cargo script`
41
+ ````sh
42
+ $ cat ./template-cargo-script.rs
43
+ #!/usr/bin/env -S cargo +nightly -Zscript
44
+ ---
45
+ [dependencies.rust-script-ext]
46
+ git = "https://github.com/kurtlawrence/rust-script-ext"
47
+ rev = "e914bc0a04e9216ffdfd15c47f4c39b74d98bbeb"
48
+ ---
49
+ // You might need to chmod +x your script!
50
+ // See <https://kurtlawrence.github.io/rust-script-ext/rust_script_ext/> for documentation
51
+ use rust_script_ext::prelude::*;
52
+
53
+ fn main() -> Result<()> {
54
+ // fastrand comes from rust_script_ext::prelude::*
55
+ let n = std::iter::repeat_with(|| fastrand::u32(1..=100))
56
+ .take(5)
57
+ .collect::<Vec<_>>();
58
+
59
+ println!("Here' s 5 random numbers: {n:? }" );
60
+ Ok(())
61
+ }
62
+ $ ./template-cargo-script.rs
63
+ Here's 5 random numbers: [91, 65, 32, 75, 39]
64
+ ` ` ` `
65
+
37
66
## Template Quickstart
38
67
39
- `template.rs` contains a simple scaffold for a `rust-script[-ext]`:
68
+ ` template-rust-script.rs` contains a simple scaffold for use with ` rust-script` :
69
+
70
+ ` ` ` sh
71
+ curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template-rust-script.rs -o my-script.rs
72
+ chmod +x my-script.rs
73
+ ./my-script.rs
74
+ ` ` `
75
+
76
+ ` template-cargo-script.rs` contains a simple scaffold for use with ` cargo-script` :
40
77
41
78
` ` ` sh
42
- curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template.rs -o my-script.rs
79
+ curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template-cargo-script .rs -o my-script.rs
43
80
chmod +x my-script.rs
44
81
./my-script.rs
45
82
` ` `
46
83
84
+ > ` cargo script` does not (currently) set up an isolated environment when running the script, which
85
+ > can cause errors if the script lives _within a Rust crate_.
86
+ > I recommend using ` rust-script` instead.
87
+
47
88
## What's included?
48
89
49
90
What ` rust-script-ext` provides is continually evolving.
@@ -55,6 +96,8 @@ If you find something lacking, I encourage you to open a PR!
55
96
56
97
## Language Server Support
57
98
99
+ > Note: only tested with ` rust-script` .
100
+
58
101
[` rscls` ](https://github.com/MiSawa/rscls/) works as a middle-man LSP between rust-analyzer and
59
102
rust-script.
60
103
Below are instructions for getting LSP support using Neovim.
0 commit comments