nu_cmd_lang/core_commands/
break_.rs

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