Skip to content

Commit 68cad3e

Browse files
committed
Standard Library 0.99 chapter
1 parent 7679879 commit 68cad3e

File tree

1 file changed

+200
-10
lines changed

1 file changed

+200
-10
lines changed

book/standard_library.md

Lines changed: 200 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Nushell ships with a standard library of useful commands written in native Nu. By default, the standard library is loaded into memory (but not automatically imported) when Nushell starts.
44

5+
[[toc]]
6+
7+
## Overview
8+
59
The standard library currently includes:
610

711
- Assertions
@@ -14,32 +18,218 @@ The standard library currently includes:
1418
To see a complete list of the commands available in the standard library, run the following:
1519

1620
```nu
17-
use std
18-
scope commands
19-
| where name =~ '^std '
20-
| select name usage extra_usage
21-
| wrap "std-lib"
21+
nu -c "
22+
use std
23+
scope commands
24+
| where name =~ '^std '
25+
| select name description extra_description
26+
| wrap 'Standard Library Commands'
27+
| table -e
28+
"
29+
```
30+
31+
::: note
32+
The `use std` command above loads the entire standard library so that you can see all of the commands at once. This is typically not how it will be used (more info below). It is also run in a separate Nu subshell simply so that it is not loaded into scope in the shell you are using.
33+
:::
34+
35+
## Importing the Standard Library
36+
37+
While working at the commandline, it can be convenient to load the entire standard library using:
38+
39+
```nu
40+
use std *
41+
```
42+
43+
However, this form should be avoided in custom commands and scripts since it has the longest load time. While the load time is only on the order of ~20-30 milliseconds on an average system, that can add up in certain scenarios.
44+
45+
::: important Optimal Startup when Using the Standard Library
46+
See the [notes below](#optimal-startup) on how to ensure that your configutation isn't loading the entire Standard Library.
47+
:::
48+
49+
### Importing Submodules
50+
51+
Each submodule of the standard library can be loaded separately. Again, for best performance, load only the submodule(s) that you need in your code.
52+
53+
There are several forms that can be used:
54+
55+
#### 1. Import the submodule
56+
57+
Examples:
58+
59+
```nu
60+
use std/iter
61+
[2 5 "4" 7] | iter filter-map {|it| $it ** 2}
62+
63+
use std/help
64+
help ls
65+
help commands
66+
```
67+
68+
This form requires that you prefix the command using the submodule name. This can be useful in two scenarios.
69+
70+
1. In the `use std/iter` example, it's important to prefix the commands with the `iter` namespace. If, on the other hand, you were to `use std/iter *`, you would shadow the built-in `find` command with the `iter` module's `find` command.
71+
72+
2. In the `use std/help` example, `help` is both the submodule name and the name of the main command it exports. Using `use std/help` allows you to access:
73+
74+
```
75+
help
76+
help aliases
77+
help modules
78+
help commands
79+
help operators
80+
help externs
81+
```
82+
83+
In this case, the `std/help` commands are meant to shadow (replace) the built-in versions.
84+
85+
Submodules that are normally imported with `use std/<submodule>`:
86+
87+
- `use std/assert`: `assert` and its subcommands
88+
- `use std/bench`: The benchmarking command `bench`
89+
- `use std/dirs`: The directory stack command `dirs` and its subcommands
90+
- `use std/input`: The `input display` command
91+
- `use std/help`: An alternative version of the `help` command and its subcommands which supports completion and other features
92+
- `use std/iters`: Additional `iters`-prefixed iteration commands. Note: See-also alternative option #3 below.
93+
- `use std/log`: The `log <subcommands>` such as `log warning <msg>`
94+
- `use std/math`: Mathematical constants such as `$math.E`. These can also be imported without a prefix using Form #2 below.
95+
96+
#### 2. Import all submodule commands without a prefix
97+
98+
For certain submodules, you will want the commands from a submodule to be available in the current namespace, so that you _can_ simply access the command by name. For instance:
99+
100+
```nu
101+
use std/formats *
102+
ls | to jsonl
103+
```
104+
105+
Submodules that are normally imported with `use std/<submodule> *`:
106+
107+
- `use std/dt *`: Additional commands for working with `date` values
108+
- `use std/formats *`: Additional `to` and `from` format conversions
109+
- `use std/math *`: The math constants without a prefix, such as `$E`. Note that the prefixed form #1 above is likely more understandable when reading and maintaining code.
110+
- `use std/xml *`: Additional commands for working with XML data
111+
112+
#### 3. Importing specific subcommands
113+
114+
As with most modules, you can choose to import only a subset of the commands. For instance, the following would import the `zip-with` command without requiring that it be called with`iter zip-with`.
115+
116+
```nu
117+
use std/iter [ zip-with ]
118+
```
119+
120+
#### 4. Not Recommended - `use std <submodule>` and `use std <submodule> *`
121+
122+
While it is _possible_ to import Standard Library submodules using a space-separated form:
123+
124+
```nu
125+
use std log
126+
use std formats *
127+
```
128+
129+
This is NOT RECOMMENDED. Using this form has the same performance impact as:
130+
131+
```nu
132+
use std *
133+
```
134+
135+
::: caution
136+
The difference is subtle, but important from a performance perspective. Using `std/<submodules>` (with a slash) is far faster than `std <submodule>` (with a space):
137+
138+
```nu
139+
bench -n 200 --pretty { nu -c "use std/formats *; exit" }
140+
# => 9ms 271µs 832ns +/- 328µs 860ns
141+
bench -n 200 --pretty { nu -c "use std formats *; exit" }
142+
# => 26ms 921µs 59ns +/- 588µs 919ns
22143
```
23144

24-
In addition, `stdlib-candidate`, found in the [nu_scripts Repository](https://github.com/nushell/nu_scripts/tree/main/stdlib-candidate/std-rfc), serves as a staging ground for new commands before they are added to the standard library.
145+
:::
146+
147+
## The Standard Library Candidate Module
148+
149+
(Also known as `std-rfc`)
150+
151+
`stdlib-candidate`, found in the [nu_scripts Repository](https://github.com/nushell/nu_scripts/tree/main/stdlib-candidate/std-rfc), serves as a staging ground for new commands before they are added to the Standard Library.
152+
153+
If you are interested in adding to the Standard Library, please submit your code via PR to the Candidate module in that repository. We also encourage you to install this module and provide feedback on upcoming candidate commands.
154+
155+
::: details More details
156+
157+
Candidate commands for the Standard Library should, in general:
158+
159+
- Have broad appeal - Be useful to a large number of users or use cases
160+
- Be well-written and clearly commented for future maintainers
161+
- Implement help comments with example usage
162+
- Have a description that explains why you feel the command should be a part of the standard library. Think of this as an "advertisement" of sorts to convince people to try the command and provide feedback so that it can be promoted in the future.
163+
164+
In order for a command to be graduated from RFC to the Standard Library, it must have:
165+
166+
- Positive feedback
167+
- Few (or no) outstanding issues and, of course, no significant issues
168+
- A PR author for the `std` submission. This does not necessarily have to be the original author of the command.
169+
- Test cases as part of the `std` submission PR
170+
171+
Ultimately a member of the core team will decide when and if to merge the command into `std` based on these criteria.
172+
173+
Of course, if a candidate command in `std-rfc` no longer works or has too many issues, it may be removed from or disabled in `std-rfc`.
25174

26-
::: tip Note
27-
Currently, parsing of the standard library impacts Nushell startup time. New commands are not being added to the library until this is resolved.
28175
:::
29176

177+
## Disabling the Standard Library
178+
30179
To disable the standard library, you can start using:
31180

32181
```nu
33182
nu --no-std-lib
34183
```
35184

36-
You will not be able to import the library using `use std *`, nor use any of its commands, if it is disabled in this way.
185+
This can be especially useful to minimize overhead when running a command in a subshell using `nu -c`. For example:
186+
187+
```nu
188+
nu --no-std-lib -n -c "$nu.startup-time"
189+
# => 1ms 125µs 10ns
190+
191+
nu -n -c "$nu.startup-time"
192+
# => 4ms 889µs 576ns
193+
```
194+
195+
You will not be able to import the library, any of its submodules, nor use any of its commands, when it is disabled in this way.
196+
197+
## Optimal Startup
198+
199+
If Nushell's startup time is important to your workflow, review your [startup configuration]([./configuration.md]) in `config.nu`, `env.nu`, and potentially others for inefficient use of the standard library. The following command should identify any problem areas:
200+
201+
```nu
202+
view files
203+
| enumerate | flatten
204+
| where filename !~ '^std'
205+
| where filename !~ '^entry'
206+
| where {|file|
207+
(view span $file.start $file.end) =~ 'use\W+std[^\/]'
208+
}
209+
```
210+
211+
Edit those files to use the recommended syntax below.
212+
:::
213+
214+
::: note
215+
If a Nushell library (e.g., from [the `nu_scripts` repository](https://github.com/nushell/nu_scripts)) is using this syntax, please report it via an issue or PR. These will be updated over time after Nushell 0.99.0 is released.
216+
217+
If a third-party module is using this syntax, please report it to the author/maintainers to update.
218+
:::
219+
220+
## Viewing Standard Library Source
37221

38222
::: tip Did You Know?
39223
Because the standard library is simply composed of [custom commands](./custom_commands.html) in [modules](./modules.html) and [submodules](./modules.html#submodules-and-subcommands), you can see the source for each command with the [`view source`](/commands/docs/view_source.md) command. For example, to view the source for the `ellie` command (with syntax highlighting):
40224

41225
```nu
42-
use std *
226+
use std/util *
43227
view source ellie | nu-highlight
44228
:::
229+
230+
## Windows Path Syntax
231+
232+
::: important
233+
Nushell on Windows supports both forward-slashes and back-slashes as the path separator. However, to ensure cross-platform support for cripts and modules using `std`, using only the forward-slash `/` when importing Standard Library submodules is highly recommended.
234+
:::
45235
```

0 commit comments

Comments
 (0)