How can I add strings and print them?

Jan

I am currently learning Rust (mostly from scratch) and now I want to add two strings together and print them out. But that is not as easy as in other languages. Here's what I've done so far (also tested with print!):

fn sayHello(id: str, msg: str) {
    println!(id + msg);
}

fn main() {
    sayHello("[info]", "this is rust!");
}

The error I get is a little bit weird.

error: expected a literal
 --> src/main.rs:2:14
  |
2 |     println!(id + msg);
  |              ^^^^^^^^

How can I solve this so that [info] this is rust will be printed out?

Shepmaster

Don't try to learn Rust without first reading the free book The Rust Programming Language and writing code alongside.

For example, you are trying to use str, which is an unsized type. You are also trying to pass a variable to println!, which requires a format string. These things are covered early in the documentation because they trip so many people up. Please make use of the hard work the Rust community has done to document these things!

All that said, here's your code working:

fn say_hello(id: &str, msg: &str) {
    println!("{}{}", id, msg);
}

fn main() {
    say_hello("[info]", "this is Rust!");
}

I also changed to use snake_case (the Rust style).

See also:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add alternating strings to filenames and renumber them pairwise?

From Dev

How can i add print feature to jsgrid

From Dev

How can I add bunch of strings to list

From Dev

How can I filter out strings that contain no numbers within them?

From Dev

How can I concatenate strings and use them as a list name?

From Dev

How can I store strings with variables in them in another Java class?

From Dev

How can I automatically add workspaces, only if I need them?

From Dev

How can I print a joined table of strings in Lua?

From Dev

How can I print a joined table of strings in Lua?

From Dev

How can I add a print method for Audited gem in Rails?

From Dev

How can I print the result of add function..?

From Dev

How can I add a print method for Audited gem in Rails?

From Dev

How can I print an array and add a string to each item in Bash

From Dev

How can I print this?

From Dev

How can i add the strings to child nodes inside a node?

From Dev

How can I add many json strings in one JsonArray?

From Dev

How can I add many json strings in one JsonArray?

From Dev

How can I add text to the end of a line that contains multiple strings?

From Dev

how can i add two strings in Autocomplete label

From Dev

How can I scan foreign characters (like ä, ß, ő, ű) and print them out to the console window as well?

From Dev

How can i print string one by character without space between them IN python2.7?

From Dev

How can I check if two strings have the same letters, but only print the common letters once?

From Dev

How can I properly print an object containing strings and multiple arrays in PowerShell?

From Dev

How can I print this 2d array of strings in this form without too much effort (java)?

From Dev

In which container can I collect Strings to show any of them

From Dev

how do I set variables and then add them

From Dev

How can I print a Maybe?

From Dev

How can I print 413284921265094656?

From Dev

How can I print 413284921265094656?

Related Related

  1. 1

    How do I add alternating strings to filenames and renumber them pairwise?

  2. 2

    How can i add print feature to jsgrid

  3. 3

    How can I add bunch of strings to list

  4. 4

    How can I filter out strings that contain no numbers within them?

  5. 5

    How can I concatenate strings and use them as a list name?

  6. 6

    How can I store strings with variables in them in another Java class?

  7. 7

    How can I automatically add workspaces, only if I need them?

  8. 8

    How can I print a joined table of strings in Lua?

  9. 9

    How can I print a joined table of strings in Lua?

  10. 10

    How can I add a print method for Audited gem in Rails?

  11. 11

    How can I print the result of add function..?

  12. 12

    How can I add a print method for Audited gem in Rails?

  13. 13

    How can I print an array and add a string to each item in Bash

  14. 14

    How can I print this?

  15. 15

    How can i add the strings to child nodes inside a node?

  16. 16

    How can I add many json strings in one JsonArray?

  17. 17

    How can I add many json strings in one JsonArray?

  18. 18

    How can I add text to the end of a line that contains multiple strings?

  19. 19

    how can i add two strings in Autocomplete label

  20. 20

    How can I scan foreign characters (like ä, ß, ő, ű) and print them out to the console window as well?

  21. 21

    How can i print string one by character without space between them IN python2.7?

  22. 22

    How can I check if two strings have the same letters, but only print the common letters once?

  23. 23

    How can I properly print an object containing strings and multiple arrays in PowerShell?

  24. 24

    How can I print this 2d array of strings in this form without too much effort (java)?

  25. 25

    In which container can I collect Strings to show any of them

  26. 26

    how do I set variables and then add them

  27. 27

    How can I print a Maybe?

  28. 28

    How can I print 413284921265094656?

  29. 29

    How can I print 413284921265094656?

HotTag

Archive