How to print function result directly?

i have this test code:

fn main() {
    let x=40;
    println!("Fibonacci of {x} is {}", fib(x));

if i move the function call "fib(40)" inside the second set of curly braces, it errors.

is it possible to do the same way like i did for variable "x"?

If you store it in a variable as well, then yes.

Only simple variables can be interpolated within the format string braces so far, as opposed to general expressions.


You are currently printing it directly, in fact. print!("{x}") is nothing more then a syntax sugar for print!("{}", x).


Not really, you can only do something like this:

fn main() {
    let x = 40;
    println!("Fibonacci of {x} is {res}", res = fib(x));

i was trying to do this

println!("Fibonacci of {x} is {fib(x)}")

for conciseness in typing but i guess Rust is still too new.


Nah, that's not how it works at all.

Rust isn't new. It's been around since 2007, making it 15 years old. 1.0 was released in 2015, 7 years ago. The ecosystem is mature, it is being used in production for many purposes, and it's even being considered for inclusion in the Linux kernel.

If you are not an experienced language designer, and you are not familiar with the history and the ongoing development of the language, you shouldn't make unfounded assumptions and write inconsiderate comments about the features and the design of Rust.

Not allowing arbitrarily complex expressions is a deliberate decision. It's not because the compiler or its authors are not smart enough to implement such a feature, but because it would be detrimental to the readability of code. This has actually been proposed on IRLO several times by people coming from some other languages, but it was rejected, and for a good reason.

"Conciseness in typing" is not an advantage in itself. We write code a lot less than we (re-)read it. You can make code as terse as you want, but then good luck understanding what it does a few weeks later. Putting expressions of arbitrarily high complexity in a string literal actually makes code less readable, due to the double context-dependence of even lexing/parsing it (what do you do with string literals in a format string?), and all the trouble it causes for both automatic tooling (e.g. IDE syntax highlighters) and human consumers of the code.

Please, just type that variable name out.


You are wrong. That is not consistency; consistency would be:

println!("Fibonacci of {40} is {fib(x)}")

Do you see where your misunderstanding is?

println!("Fibonacci of {x} is {fib(x)}")

is easy to read than

println!("Fibonacci of {} is {}", x, fib(x))

Depends on what do you mean by that. Easier on the eyes? Probably. Easier to understand how it works? No.

Normally you wound't expect println! to change the state of anything around it.

And current state of the affairs guarantees it if your println! call have just one argument.

Large program are not read like small ones. It's highly unlikely that anyone would want to read millions lines of the code one line after another. You want strange constructs to stick out.

And calling function from println! is a strange contruct in my book.

And “strange markup” between braces is usually a way to alter output (your specify width of the field or request hexadecimal in there).

Perhaps some non-destructive operations (like array access) may be allowed, too, but I'm not sure if that's win or not: they couldn't affect the object (like function call may), but would still clash with the use of markup.

Ability to call functions? Please, no.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.