Description
Take this code:
fn main()
{
let mut x = 1;
let y = &mut x;
for _ in 0..10
{
x+=1;
}
for _ in 0..10
{
*y+=1;
}
println!("{}",*y)
}
It compiles.
You can comment both increment lines with // comments:
fn main()
{
let mut x = 1;
let y = &mut x;
for _ in 0..10
{
//x+=1;
}
for _ in 0..10
{
//*y+=1;
}
println!("{}",*y)
}
It compiles.
You can comment both for loops with /**/ comments:
fn main()
{
let mut x = 1;
let y = &mut x;
/*for _ in 0..10
{
x+=1;
}*/
/*for _ in 0..10
{
*y+=1;
}*/
println!("{}",*y)
}
It compiles.
You can mix /**/ comment on for loops and increment lines:
fn main()
{
let mut x = 1;
let y = &mut x;
/*for _ in 0..10
{
/*x+=1;*/
}*/
/*for _ in 0..10
{
/**y+=1;*/
}*/
println!("{}",*y)
}
It compiles.
You can also mix /**/ and // comments on the first for loop and its increment:
fn main()
{
let mut x = 1;
let y = &mut x;
/*for _ in 0..10
{
//x+=1;
}*/
for _ in 0..10
{
*y+=1;
}
println!("{}",*y)
}
It compiles.
But if you try to mix /**/ and // comments on the second for loop and its increment:
fn main()
{
let mut x = 1;
let y = &mut x;
for _ in 0..10
{
x+=1;
}
/*for _ in 0..10
{
//*y+=1;
}*/
println!("{}",*y)
}
It does not compile
error[E0758]: unterminated block comment
--> src/main.rs:9:5
|
9 | / /*for _ in 0..10
10 | | {
11 | | //*y+=1;
12 | | }*/
13 | | println!("{}",*y)
14 | | }
| |__^
error: aborting due to previous error
It seems that, when parsing a /**/ comment, rustc does not recognize // comments. This is not an issue in general but in the particular case where // is followed by a dereference operator * ("//*"), then rustc reads "/*" and takes this as another level of /**/ comment.
I am not an expert in Rust and I know that /**/ comments are not idiomatic, but I still think this should not happen.
I noticed that my syntax highlighter (KSyntaxHighlighting) makes the same mistake, but it is likely that it is still a bug that happen to be present in both, rustc and the Rust language description of KSyntaxHighlighting (same with GitHub's). Maybe I am wrong and this is intended but, in that case, I would like to know. I have read nothing about mixing /**/ and // comments but if it works in most cases, having particular cases where it does not work is a confusing behavior.