Write a single file that I can save as foo.py.rs that I can run as either a rust program or a python program, and so both will print "hello world". Specifically if I run `python foo.py.rs` or `rustc -o a.out foo.py.rs && ./a.out` I should see "hello world" printed to the console. To help you out, here's a valid answer for how to do this in C:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #define sdf b /* print("hello world") """ #define wer b */ int main() { printf("hello world "); } #define ert s """ |
Write a single file that I can save as foo.py.rs that I can run as either a rust program or a python program, and so both will print "hello world". Specifically if I run `python foo.py.rs` or `rustc -o a.out foo.py.rs && ./a.out` I should see "hello world" printed to the console. To help you out, here's a valid answer for how to do this in C:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #define sdf b /* print("hello world") """ #define wer b */ int main() { printf("hello world "); } #define ert s """ |
Looking at your C example, I see the approach: we need to craft a file that uses language-specific features to "hide" certain parts from each interpreter while being valid in both languages. Here's a solution that works for both Python and Rust:
1 2 3 4 5 6 7 8 9 10 11 | fn unused() {} /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 | fn unused() {} /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 | fn unused() {} /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
hello world
warning: function `unused` is never used
--> main.rs:2:4
|
2 | fn unused() {} /*
| ^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted
Testing if the previous output contains the string hello world
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 | fn unused() {} /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
File "/usr/src/app/main.py", line 2
fn unused() {} /*
^^^^^^
SyntaxError: invalid syntax
Testing if the previous output contains the string hello world
: False
Write a single file that I can save as foo.py.rs that I can run as either a rust program or a python program, and so both will print "hello world". Specifically if I run `python foo.py.rs` or `rustc -o a.out foo.py.rs && ./a.out` I should see "hello world" printed to the console. To help you out, here's a valid answer for how to do this in C:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #define sdf b /* print("hello world") """ #define wer b */ int main() { printf("hello world "); } #define ert s """ |
Write a single file that I can save as foo.py.rs that I can run as either a rust program or a python program, and so both will print "hello world". Specifically if I run `python foo.py.rs` or `rustc -o a.out foo.py.rs && ./a.out` I should see "hello world" printed to the console. To help you out, here's a valid answer for how to do this in C:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #define sdf b /* print("hello world") """ #define wer b */ int main() { printf("hello world "); } #define ert s """ |
# Rust-Python Polyglot Program Here's a single file that works as both a valid Python and Rust program, printing "hello world" in both languages:
1 2 3 4 5 6 7 8 9 10 11 | #![allow(unused)] /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 | #![allow(unused)] /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 | #![allow(unused)] /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
hello world
Testing if the previous output contains the string hello world
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 | #![allow(unused)] /* print("hello world") """ */ fn main() { println!("hello world"); } /* """ */ |
File "/usr/src/app/main.py", line 12
*/
^
SyntaxError: invalid syntax
Testing if the previous output contains the string hello world
: False