Understanding Move Through a Rustacean’s Lens

Introduction to Rust and Move: A Comparative Overview
Rust and Move are both modern programming languages that prioritize safety, performance, and concurrency. Rust is widely known for systems programming and its strong memory safety guarantees. Move, on the other hand, was created specifically for blockchain smart contracts, with a strong emphasis on resource safety and determinism.
While Rust is used in a wide range of industries, including embedded systems, web assembly, and systems programming, Move is tailored specifically to the blockchain world, designed to handle secure and efficient smart contracts.
In this guide, we’ll explore their key features, look at how they compare in certain domains, and discuss the practical aspects of moving from Rust to Move.
Key Features of Rust: What Makes It Stand Out?
Memory Safety Without Garbage Collection
Rust ensures memory safety through its ownership model, where each piece of data has a clear owner, and the compiler enforces strict rules about how data is accessed and modified. This eliminates common bugs like null pointer dereferencing, buffer overflows, and data races without relying on a garbage collector.
Example: Rust Ownership Model
rustCopyEditfn main() {
let s1 = String::from("Hello, world!"); // s1 owns the string
let s2 = s1; // Ownership moves from s1 to s2
// println!("{}", s1); // This would cause an error because s1 no longer owns the string
println!("{}", s2); // This is fine
}
In Rust, ownership and borrowing rules are enforced by the compiler, ensuring that memory management is handled safely and efficiently without needing a garbage collector.
Concurrency without Data Races
Rust’s concurrency model ensures that threads cannot access the same memory location at the same time unless one of them is read-only. This prevents data races and makes concurrent programming both safer and easier.
Understanding Move: A New Language for Blockchain
Move was originally created for the Diem blockchain and is now being used by platforms like Aptos and Sui. Unlike general-purpose languages like Rust, Move was designed specifically for writing smart contracts on blockchains, focusing on resource safety and transactional integrity.
Key Features of Move:
Resource-Oriented Programming: Move uses a resource model that ensures that resources (like tokens or NFTs) are only accessible by one entity at a time. This prevents issues like double-spending and ensures that assets are safely transferred between addresses.
Deterministic Execution: Move does not support asynchronous programming, which is common in languages like Rust. All operations must be deterministic to ensure the contract execution is the same across all nodes in the network.
Built-In Security: Move’s design ensures that contracts cannot have unexpected side effects. For example, trying to create a resource from a non-zero value will throw an error, ensuring resource consistency.
Example: Move Resource Transfer
moveCopyEditmodule MyToken {
struct Token has store {
balance: u64
}
public entry fun transfer(
sender: &signer,
receiver: address,
amount: u64
) {
let sender_balance = borrow_global_mut<Token>(sender).balance;
assert(sender_balance >= amount, 0);
sender_balance = sender_balance - amount;
move_to(receiver, Token { balance: amount });
}
}
In this example:
borrow_global_mut
retrieves a mutable reference to the token.move_to
transfers the token resource to the receiver.
Transitioning from Rust to Move: What You Need to Know
Ownership vs Resources
One of the key similarities between Rust and Move is the ownership model. Rust’s ownership system ensures that data is either owned by one party or borrowed temporarily. Similarly, in Move, resources cannot be copied or discarded unless explicitly transferred, making it highly secure in blockchain environments.
Error Handling: Rust’s Results vs Move’s Abort
In Rust, errors are handled using the Result<T, E>
type, which provides detailed error messages. Move uses a more simplified approach with abort
codes, which return a generic error code and stop the program’s execution.
Rust Error Handling Example:
rustCopyEditfn divide(a: u64, b: u64) -> Result<u64, String> {
if b == 0 {
Err(String::from("Cannot divide by zero"))
} else {
Ok(a / b)
}
}
Move Error Handling:
moveCopyEditfun safe_divide(a: u64, b: u64): u64 {
if (b == 0) {
abort 0; // Abort with code 0
}
a / b
}
Hands-On Comparison: Try Move and Rust Side by Side
Write and compare the same logic in both Rust and Move to understand the key differences in syntax, ownership, mutability, and modularity.
1. Variable Declaration and Mutability:
Rust
rustCopyEditfn main() {
let mut x = 10;
println!("x before: {}", x);
x = 20;
println!("x after: {}", x);
}
Move
moveCopyEditmodule VariableExample {
fun demo_variable(): u64 {
let mut x = 10;
x = 20;
x
}
}
Key Insight: Both support mut
for mutable bindings. Move enforces stricter ownership semantics, especially in smart contract contexts.
2. Functions and Return Types:
Rust
rustCopyEditfn square(x: i32) -> i32 {
x * x
}
fn main() {
println!("Square: {}", square(4));
}
Move
moveCopyEditmodule Math {
public fun square(x: u64): u64 {
x * x
}
}
Key Insight: Move uses public fun
for exported functions in a module. Primitive types like u64
work similarly in both languages.
3. Struct Definition and Instantiation:
Rust
rustCopyEditstruct User {
name: String,
age: u8,
}
fn main() {
let u = User {
name: String::from("Alice"),
age: 30,
};
println!("Name: {}, Age: {}", u.name, u.age);
}
Move
moveCopyEditmodule UserModule {
struct User has copy, drop {
name: vector<u8>,
age: u8,
}
public fun create_user(): User {
User {
name: b"Alice".to_vec(),
age: 30,
}
}
}
Key Insight: In Move, struct types must declare their abilities (like copy
, drop
, or store
). These are critical for resource safety.
4. Loops and Conditionals:
Rust
rustCopyEditfn main() {
for i in 0..5 {
if i % 2 == 0 {
println!("Even: {}", i);
}
}
}
Move
moveCopyEditmodule LoopModule {
public fun print_even() {
let i = 0;
while (i < 5) {
if (i % 2 == 0) {
// Simulate logic or emit events
};
i = i + 1;
}
}
}
Key Insight: Move does not support direct printing. Logging or event emission is handled via libraries such as Std::debug
.
5. Ownership and Borrowing (Conceptual Difference):
Rust
rustCopyEditfn take_ownership(s: String) {
println!("Took: {}", s);
}
fn main() {
let s = String::from("hello");
take_ownership(s);
// s is moved, cannot be used here
}
Move
moveCopyEditmodule OwnershipExample {
fun consume(val: u64): u64 {
val
}
}
Key Insight: Move enforces linear ownership by default. Values behave like resources unless explicitly marked as copy
.
Practical Applications: Where Rust and Move Shine
Rust’s Strengths:
Systems Programming: Rust’s performance and memory safety make it ideal for operating systems, embedded systems, and web assembly.
Concurrency: With its thread-safety guarantees, Rust is widely used in applications requiring parallelism and multi-threading.
Move’s Strengths:
Blockchain Smart Contracts: Move is uniquely suited for secure and verifiable blockchain applications, where asset management and transaction integrity are critical.
Digital Asset Management: Its resource-oriented model makes it perfect for handling digital assets (e.g., tokens, NFTs) in a decentralized manner.
Conclusion: Choosing the Right Language for Your Project
Choosing between Rust and Move largely depends on the type of project you are working on:
Rust is the go-to language for systems programming, performance-sensitive applications, and concurrent programming.
Move, on the other hand, is specifically tailored for blockchain applications, especially those focused on resource safety, smart contract development, and digital asset management.
As someone transitioning from Rust to Move, I can say that Move’s approach to resource safety is refreshing but also limiting in certain areas. If you're building a blockchain application that requires high levels of security and efficiency, Move is an excellent choice. If you are more interested in general-purpose programming or performance optimization, Rust is unmatched.
Resources for Learning Rust and Move
Rust:
Rust Official Site: Complete docs, tutorials, and tooling guides.
The Rust Programming Book: The definitive beginner-to-advanced guide.
Rustlings: Small, interactive coding exercises.
Rust By Example: Learn Rust via runnable examples.
Rust Playground: Write and run Rust code in your browser.
Move:
Move Language Site: Official docs and language guide.
The Move Book: Covers basics to advanced smart contracts.
Aptos Dev Portal: Aptos-specific Move tutorials and APIs.
Sui Dev Resources: Sui guides, tooling, and examples.
Move Playground: Try Move code in-browser.
Blockchain Development:
Blockchain at Berkeley: Free blockchain courses.
CryptoZombies: Fun Solidity tutorials, good for cross-framework concepts.
Dapp University: Learn dApp development across chains.
Solana Dev Resources: Comprehensive Solana development hub.
Blockchain Developer Guide: Platform-agnostic blockchain learning.
Communities:
Rust Forum & r/rust: Ask questions, get feedback, share progress.
Move Discord: Official Move dev community.
Aptos Discord: Collaborate with Aptos builders.
Sui Discord: Engage with Sui’s Move-based dev community.
Subscribe to my newsletter
Read articles from Vasvi Garg directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
