Welcome to the first article in the series on learning Rust from a Pythonista’s perspective. In this post, we’ll dive into the fundamental syntax and structure of Rust, drawing comparisons to Python to ease the learning curve. This will help you get familiar with Rust’s core language features by relating them to what you already know.
In Python, functions are defined with def, and you have the freedom of not explicitly defining types for parameters or return values:
def add(x, y):
return x + y
In Rust, functions are defined using the fn keyword. Unlike Python, Rust enforces explicit type annotations for both parameters and return values:
fn add(x: i32, y: i32) -> i32 {
x + y
}
In this Rust example, the types of x and y are explicitly declared as i32, which is a 32-bit integer, and the return type is also specified after the -> symbol. Rust’s strict type system ensures safety and performance at compile time, something Python typically handles dynamically at runtime.
->). Additionally, Rust doesn’t use the return keyword if the last expression in the function is the return value. Python, on the other hand, always uses return.In Python, variables are mutable by default:
x = 10
x = x + 5
In Rust, variables are immutable by default. If you want to make a variable mutable, you have to explicitly declare it using the mut keyword:
let mut x = 10;
x = x + 5;
mut) if you plan to change their values.if StatementsPython and Rust both use if statements for conditionals, but there are some differences in syntax and flexibility.
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
let x = 10;
if x > 5 {
println!("x is greater than 5");
} else {
println!("x is less than or equal to 5");
}
{} around blocks of code.for and whilePython’s for loop iterates over sequences like lists or ranges, while Rust’s for loop works similarly but is often paired with iterators.
for i in range(5):
print(i)
for i in 0..5 {
println!("{}", i);
}
range(5) generates numbers from 0 to 4. In Rust, the range syntax 0..5 also generates values from 0 to 4. Rust’s range syntax is more flexible and can be inclusive (0..=5 includes 5).Rust also has a while loop similar to Python’s:
i = 0
while i < 5:
print(i)
i += 1
let mut x = 0;
while x < 5 {
println!("{}", x);
x += 1;
}
i as mutable (mut) if you plan to change its value, while Python doesn’t require such an explicit declaration.if StatementsOne unique feature of Rust is that if statements are expressions, meaning they can return values. Python’s if blocks are statements and cannot return values directly without using additional techniques like a ternary operator.
x = 5
y = 10 if x > 5 else 0
let x = 5;
let y = if x > 5 { 10 } else { 0 };
if can return values directly, making it more expressive in some cases compared to Python.if is already an expression.main FunctionIn Rust, every standalone program requires a main function, much like a script in Python. However, Python doesn’t require this unless the script needs to be run in a specific context:
if __name__ == "__main__":
print("Hello, Python!")
fn main() {
println!("Hello, Rust!");
}
main function as an entry point, whereas Python scripts can be run without one. The if __name__ == "__main__" guard is a common idiom in Python but not necessary in Rust.The basics of Rust syntax and structure are not drastically different from Python, but Rust’s strict typing, immutability by default, and expression-based control flow may feel unfamiliar at first. Understanding these foundational differences will set you up for success as you dive deeper into Rust’s powerful features.
In the next article, we’ll explore Rust’s memory management model, focusing on Ownership and Borrowing, which is quite different from Python’s garbage collection system. Stay tuned!
// 1. Defining Functions in Rust
fn add(x: i32, y: i32) -> i32 {
x + y
}
fn main() {
// Call the add function
println!("Sum of 2 and 3: {}", add(2, 3));
// 2. Variables and Mutability
let mut x = 10;
x = x + 5;
println!("Updated value of x: {}", x);
// 3. Control Flow: if Statements
if x > 5 {
println!("x is greater than 5");
} else {
println!("x is less than or equal to 5");
}
// 4. Loops in Rust: for Loop
println!("For loop output:");
for i in 0..5 {
println!("{}", i);
}
// 4. Loops in Rust: while Loop
println!("While loop output:");
let mut y = 0;
while y < 5 {
println!("{}", y);
y += 1;
}
// 5. Returning values from if statements
let z = if x > 5 { 10 } else { 0 };
println!("Value of z: {}", z);
// 6. Rust's main function (Already part of the example)
println!("Hello, Rust!");
}
add function is defined and called with parameters 2 and 3, returning their sum.x is modified and printed.if statement checks whether x is greater than 5 and prints the appropriate message.for loop prints numbers from 0 to 4, and a while loop does the same for y from 0 to 4.if: The result of the if expression is stored in z and printed.You can compile and run this Rust code to verify the output.
main.rs.rustc main.rs
./main
If you're eager to continue this learning journey and stay updated with the latest insights, consider subscribing. By joining our mailing list, you'll receive notifications about new articles, tips, and resources to help you seamlessly pick up Rust by leveraging your Python skills.
000 - Learning Rust as a Pythonista: A Suggested Path
001 - Learning Rust as a Pythonista: How to Create and Run a Rust File
006 - Rust Traits vs. Python Duck Typing: A Comparison for Pythonistas
007 - Concurrency in Rust for Python Developers