nu_cmd_lang/core_commands/
mut_.rs1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Mut;
6
7impl Command for Mut {
8 fn name(&self) -> &str {
9 "mut"
10 }
11
12 fn description(&self) -> &str {
13 "Create a mutable variable and give it a value."
14 }
15
16 fn signature(&self) -> nu_protocol::Signature {
17 Signature::build("mut")
18 .input_output_types(vec![(Type::Any, Type::Nothing)])
19 .allow_variants_without_examples(true)
20 .required("var_name", SyntaxShape::VarWithOptType, "Variable name.")
21 .required(
22 "initial_value",
23 SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::MathExpression)),
24 "Equals sign followed by value.",
25 )
26 .category(Category::Core)
27 }
28
29 fn extra_description(&self) -> &str {
30 r#"This command is a parser keyword. For details, check:
31 https://www.nushell.sh/book/thinking_in_nu.html"#
32 }
33
34 fn command_type(&self) -> CommandType {
35 CommandType::Keyword
36 }
37
38 fn search_terms(&self) -> Vec<&str> {
39 vec!["set", "mutable"]
40 }
41
42 fn run(
43 &self,
44 _engine_state: &EngineState,
45 _stack: &mut Stack,
46 _call: &Call,
47 _input: PipelineData,
48 ) -> Result<PipelineData, ShellError> {
49 eprintln!(
52 "Tried to execute 'run' for the 'mut' command: this code path should never be reached in IR mode"
53 );
54 unreachable!()
55 }
56
57 fn examples(&self) -> Vec<Example<'_>> {
58 vec![
59 Example {
60 description: "Set a mutable variable to a value, then update it",
61 example: "mut x = 10; $x = 12",
62 result: None,
63 },
64 Example {
65 description: "Upsert a value inside a mutable data structure",
66 example: "mut a = {b:{c:1}}; $a.b.c = 2",
67 result: None,
68 },
69 Example {
70 description: "Set a mutable variable to the result of an expression",
71 example: "mut x = 10 + 100",
72 result: None,
73 },
74 Example {
75 description: "Set a mutable variable based on the condition",
76 example: "mut x = if false { -1 } else { 1 }",
77 result: None,
78 },
79 ]
80 }
81}
82
83#[cfg(test)]
84mod test {
85 use nu_protocol::engine::CommandType;
86
87 use super::*;
88
89 #[test]
90 fn test_examples() {
91 use crate::test_examples;
92
93 test_examples(Mut {})
94 }
95
96 #[test]
97 fn test_command_type() {
98 assert!(matches!(Mut.command_type(), CommandType::Keyword));
99 }
100}