Repository: nushell/nushell
File: crates/nu-cmd-lang/src/core_commands/echo.rs (Rust)

Code before:
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let mut args = call.rest(engine_state, stack, 0)?;
        let value = match args.len() {
            0 => Value::string("", call.head),
            1 => args.pop().expect("one element"),
            _ => Value::list(args, call.head),
        };
        Ok(value.into_pipeline_data())
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let mut args = call.rest_const(working_set, 0)?;
        let value = match args.len() {
            0 => Value::string("", call.head),
            1 => args.pop().expect("one element"),
            _ => Value::list(args, call.head),
        };
        Ok(value.into_pipeline_data())
    }

    fn is_const(&self) -> bool {
        true
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Put a list of numbers in the pipeline. This is the same as [1 2 3].",
                example: "echo 1 2 3",
                result: Some(Value::list(
                    vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
                    Span::test_data(),
                )),
            },
            Example {
                description:
                    "Returns the piped-in value, by using the special $in variable to obtain it.",
                example: "echo $in",

Review comment (refactor):
Thanks!  I think it's good to move duplicate code inside both `run` and `run_const` into a new function.
```rust
fn echo_impl(args: Vec<Value>) -> Result<PipelineData, ShellError> {
    let value = match args.len() {
            0 => Value::string("", call.head),
            1 => args.pop().expect("one element"),
            _ => Value::list(args, call.head),
        };
        Ok(value.into_pipeline_data())
}
```

Code after:
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let args = call.rest(engine_state, stack, 0)?;
        echo_impl(args, call.head)
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let args = call.rest_const(working_set, 0)?;
        echo_impl(args, call.head)
    }

    fn is_const(&self) -> bool {
        true
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Put a list of numbers in the pipeline. This is the same as [1 2 3].",
                example: "echo 1 2 3",
                result: Some(Value::list(
                    vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
                    Span::test_data(),
                )),
            },
            Example {
                description:
                    "Returns the piped-in value, by using the special $in variable to obtain it.",
                example: "echo $in",
                result: None,
            },
        ]
    }
}

fn echo_impl(mut args: Vec<Value>, head: Span) -> Result<PipelineData, ShellError> {
    let value = match args.len() {
        0 => Value::string("", head),
        1 => args.pop().expect("one element"),