We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
I agree about the 101. In this case a sightly better form would be to use the inclusive range:
for x in 1..=100 {
....
}
If we were to simply write println!(str), we would get a compile error due to type mismatch. The best short and simple explanation of this I could find was on a stack overflow post:
'"foo" is a string literal, 8 is a numeric literal. let s = "foo" is a statement that assigns the value of a string literal to an identifier (variable). println!(s) is a statement that provides an identifier to the macro.'
That only half explains it. For example, this returns an error
fn main(){
let s = "foo";
println!(s);
}
error: expected a literal
--> src/bin/datatypes.rs:5:14
|
5 | println!(s);
| ^
Sure you could do println!("foo")
, i.e. passing a string literal, but often you want to interleave variables in the string like println!("my value is {}", s)
The println macro expects 1 or more arguments, and the first must be a string literal
That was an excellent tutorial! As a programmer who's been kind of intimidated by rust, I now feel like I could bust out a script the same way I do in C! Thanks for the help!
Hi Lil, thanks for this nice tutorial. There's a small typo in the first line: "It was originially" -> originally
Cheers!
I have two comments: 1) Using `x++;` in the `loop` construct but changing it to `x += 1;` in the `while` loop could be a potential source of confusion for novices. It might be best to either make them both the same or comment examples to explain that `x` can be increased by 1 either way. And 2) It might also be confusing to novices to see the `101` in the upper limit of the `for` loop since they might not know that it only loops to one less than the upper limit