diff --git a/crates/nu-cmd-lang/src/core_commands/echo.rs b/crates/nu-cmd-lang/src/core_commands/echo.rs index d4ebeb2aa8..6ee46153e5 100644 --- a/crates/nu-cmd-lang/src/core_commands/echo.rs +++ b/crates/nu-cmd-lang/src/core_commands/echo.rs @@ -34,13 +34,22 @@ little reason to use this over just writing the values as-is."# call: &Call, _input: PipelineData, ) -> Result { - 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()) + 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 { + let args = call.rest_const(working_set, 0)?; + echo_impl(args, call.head) + } + + fn is_const(&self) -> bool { + true } fn examples(&self) -> Vec { @@ -63,6 +72,15 @@ little reason to use this over just writing the values as-is."# } } +fn echo_impl(mut args: Vec, head: Span) -> Result { + let value = match args.len() { + 0 => Value::string("", head), + 1 => args.pop().expect("one element"), + _ => Value::list(args, head), + }; + Ok(value.into_pipeline_data()) +} + #[cfg(test)] mod test { #[test] diff --git a/crates/nu-command/tests/commands/echo.rs b/crates/nu-command/tests/commands/echo.rs index 44e6d81459..a03928d4e3 100644 --- a/crates/nu-command/tests/commands/echo.rs +++ b/crates/nu-command/tests/commands/echo.rs @@ -34,3 +34,9 @@ fn echo_range_handles_exclusive_down() { assert_eq!(actual.out, "[3,2]"); } + +#[test] +fn echo_is_const() { + let actual = nu!(r#"const val = echo 1..3; $val | to json --raw"#); + assert_eq!(actual.out, "[1,2,3]"); +}