Skip to main content

nu_cmd_lang/core_commands/
mut_.rs

1use 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(
21                "var_name",
22                SyntaxShape::VarWithOptType,
23                "The mutable variable name to create.",
24            )
25            .required(
26                "initial_value",
27                SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::MathExpression)),
28                "Equals sign followed by value.",
29            )
30            .category(Category::Core)
31    }
32
33    fn extra_description(&self) -> &str {
34        r#"This command is a parser keyword. For details, check:
35  https://www.nushell.sh/book/thinking_in_nu.html"#
36    }
37
38    fn command_type(&self) -> CommandType {
39        CommandType::Keyword
40    }
41
42    fn search_terms(&self) -> Vec<&str> {
43        vec!["set", "mutable"]
44    }
45
46    fn run(
47        &self,
48        _engine_state: &EngineState,
49        _stack: &mut Stack,
50        _call: &Call,
51        _input: PipelineData,
52    ) -> Result<PipelineData, ShellError> {
53        // This is compiled specially by the IR compiler. The code here is never used when
54        // running in IR mode.
55        eprintln!(
56            "Tried to execute 'run' for the 'mut' command: this code path should never be reached in IR mode"
57        );
58        unreachable!()
59    }
60
61    fn examples(&self) -> Vec<Example<'_>> {
62        vec![
63            Example {
64                description: "Set a mutable variable to a value, then update it.",
65                example: "mut x = 10; $x = 12",
66                result: None,
67            },
68            Example {
69                description: "Upsert a value inside a mutable data structure.",
70                example: "mut a = {b:{c:1}}; $a.b.c = 2",
71                result: None,
72            },
73            Example {
74                description: "Set a mutable variable to the result of an expression.",
75                example: "mut x = 10 + 100",
76                result: None,
77            },
78            Example {
79                description: "Set a mutable variable based on the condition.",
80                example: "mut x = if false { -1 } else { 1 }",
81                result: None,
82            },
83        ]
84    }
85}
86
87#[cfg(test)]
88mod test {
89    use nu_protocol::engine::CommandType;
90
91    use super::*;
92
93    #[test]
94    fn test_examples() {
95        use crate::test_examples;
96
97        test_examples(Mut {})
98    }
99
100    #[test]
101    fn test_command_type() {
102        assert!(matches!(Mut.command_type(), CommandType::Keyword));
103    }
104}