[rust by example] Why this error

fn main() {

    fn main() {

        // In general, the `{}` will be automatically replaced with any

        // arguments. These will be stringified.

        println!("{} days", 31);

    

        // Without a suffix, 31 becomes an i32. You can change what type 31 is

        // by providing a suffix. The number 31i64 for example has the type i64.

    

        // There are various optional patterns this works with. Positional

        // arguments can be used.

        println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");

    

        // As can named arguments.

        println!("{subject} {verb} {object}",

                 object="the lazy dog",

                 subject="the quick brown fox",

                 verb="jumps over");

    

        // Special formatting can be specified after a `:`.

        println!("{} of {:b} people know binary, the other half doesn't", 1, 2);

    

        // You can right-align text with a specified width. This will output

        // "     1". 5 white spaces and a "1".

        println!("{number:>width$}", number=1, width=6);

    

        // You can pad numbers with extra zeroes. This will output "000001".

        println!("{number:>0width$}", number=1, width=6);

    

        // Rust even checks to make sure the correct number of arguments are

        // used.

        println!("My name is {0}, {1} {0}", "James",Bond");

        // FIXME ^ Add the missing argument: "James"

    

        // Create a structure named `Structure` which contains an `i32`.

        #[allow(dead_code)]

        struct Structure(i32);

    

        // However, custom types such as this structure require more complicated

        // handling. This will not work.

        // println!("This struct `{}` won't print...", Structure(3));

        // FIXME ^ Comment out this line.

    }

}

why do I still get a error on the comment out line ?

The println!(...) in line 63 has one of the string literals ("Bond") is missing a " which “inverts” the strings and non-strings that follow it. (I.e. ever " that is supposed to start a string now ends a string and vice versa.) The last error message is the important one:

error[E0765]: unterminated double quote string
  --> src/main.rs:81:53
   |
81 |           // println!("This struct `{}` won't print...", Structure(3));
   |  _____________________________________________________^
82 | |
83 | |         // FIXME ^ Comment out this line.
84 | |
85 | |     }
86 | |
87 | | }
   | |_^

Once you terminate the double quote in some way (e.g. like this), you get more helpful error messages including some pointing to the right line:

error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, or an operator, found `");

        // FIXME ^ Add the missing argument: "James`
  --> src/main.rs:63:57
   |
63 |           println!("My name is {0}, {1} {0}", "James",Bond");
   |                                                           ^ expected one of 7 possible tokens
   |  _________________________________________________________|
   | |
64 | |
65 | |         // FIXME ^ Add the missing argument: "James"
   | |___________________________________________________^ unexpected token

The biggest help in your case actually comes from syntax highlighting. If your code is hightlighted you can see the messed up string literals in the final part of your program, e.g. on this very forum post by the red color of the string literals. One thing to be aware of is that in Rust string literals can span multiple lines, and the line breaks are interpreted as line breaks, e.g.:

fn main() {
    println!(
"Hello World
we can write
multiple lines!"
    );
}

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.