Interactive Playground
Practice Rust fundamentals. Powered by play.rust-lang.org (actix_web crate not available in sandbox).
src/main.rsVerified Example
1// 🦀 Rust Playground — Powered by play.rust-lang.org
2// Note: actix_web crate is not available here.
3// Use this to practice Rust fundamentals!
4
5fn main() {
6 // --- Ownership & Borrowing ---
7 let greeting = String::from("Hello from ActixWeb Playground!");
8 print_message(&greeting);
9 println!("Original: {}", greeting);
10
11 // --- Structs & Methods ---
12 let user = User::new("rustacean", "rust@actixweb.com");
13 println!("{}", user.summary());
14
15 // --- Enums & Pattern Matching ---
16 let status = HttpStatus::Ok;
17 println!("Status {}: {}", status.code(), status.message());
18
19 // --- Iterators & Closures ---
20 let routes = vec!["/", "/api/users", "/api/posts", "/health"];
21 let api_routes: Vec<&&str> = routes
22 .iter()
23 .filter(|r| r.starts_with("/api"))
24 .collect();
25 println!("API routes: {:?}", api_routes);
26}
27
28fn print_message(msg: &str) {
29 println!("📨 {}", msg);
30}
31
32struct User {
33 username: String,
34 email: String,
35}
36
37impl User {
38 fn new(username: &str, email: &str) -> Self {
39 User {
40 username: username.to_string(),
41 email: email.to_string(),
42 }
43 }
44
45 fn summary(&self) -> String {
46 format!("👤 {} <{}>", self.username, self.email)
47 }
48}
49
50enum HttpStatus {
51 Ok,
52 NotFound,
53 InternalError,
54}
55
56impl HttpStatus {
57 fn code(&self) -> u16 {
58 match self {
59 HttpStatus::Ok => 200,
60 HttpStatus::NotFound => 404,
61 HttpStatus::InternalError => 500,
62 }
63 }
64
65 fn message(&self) -> &str {
66 match self {
67 HttpStatus::Ok => "OK",
68 HttpStatus::NotFound => "Not Found",
69 HttpStatus::InternalError => "Internal Server Error",
70 }
71 }
72}
73
Line-by-Line Explanation
Select a line of code to understand what it does.
Output Console
Press Run to compile and execute in the sandbox.