Skip to main content

nu_cmd_lang/core_commands/
return_.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Return;
6
7impl Command for Return {
8    fn name(&self) -> &str {
9        "return"
10    }
11
12    fn description(&self) -> &str {
13        "Return early from a custom command."
14    }
15
16    fn signature(&self) -> nu_protocol::Signature {
17        Signature::build("return")
18            .input_output_types(vec![(Type::Nothing, Type::Any)])
19            .optional(
20                "return_value",
21                SyntaxShape::Any,
22                "Optional value to return.",
23            )
24            .category(Category::Core)
25    }
26
27    fn extra_description(&self) -> &str {
28        r#"This command is a parser keyword. For details, check:
29  https://www.nushell.sh/book/thinking_in_nu.html"#
30    }
31
32    fn command_type(&self) -> CommandType {
33        CommandType::Keyword
34    }
35
36    fn run(
37        &self,
38        _engine_state: &EngineState,
39        _stack: &mut Stack,
40        _call: &Call,
41        _input: PipelineData,
42    ) -> Result<PipelineData, ShellError> {
43        // This is compiled specially by the IR compiler. The code here is never used when
44        // running in IR mode.
45        eprintln!(
46            "Tried to execute 'run' for the 'return' command: this code path should never be reached in IR mode"
47        );
48        unreachable!()
49    }
50
51    fn examples(&self) -> Vec<Example<'_>> {
52        vec![Example {
53            description: "Return early.",
54            example: r#"def foo [] { return }"#,
55            result: None,
56        }]
57    }
58}