Aptos Move: Strings and Essential Debugging Tips

Moncayo Labs
Cryptocurrency Scripts
3 min readFeb 12, 2024

Mastering String Output and Debugging on Aptos: Effective Techniques Revealed

When you first start off developing on Aptos, the big question is: what’s the Solidity equivalent forimport hardhat/console.sol and console.log() on Aptos?! Without the [debug] printouts, effective Aptos software development would be incredibly troublesome and difficult.

Resources on Aptos are scarce, so here’s a tiny intro to debugging and how to print strings in Move for the Aptos blockchain.

Printing Numbers to the Console in Aptos

In Aptos Move, debugging is done via the std::debug library, imported at the top of your module (use std::debug;). It is the equivalent to console.log() in TypeScript or hardhat/console.sol for Solidity EVM blockchain code.

Output of running aptos move test with debug statements

Please note that whenever you want to print something to the console in Aptos Move, using the std::debug library, you must print the reference of the variable &var you are trying to print to the console. For instance:

use std::debug;

public entry fun print_something() {
let my_number: u64 = 1;
debug::print(&my_number);
}

Printing Strings to the Console in Aptos

Strings using the String library

Strings require more care when used as a data structure on Aptos. Similar to other low-level programming languages like C++, Move lacks a native string type. Thus, if one wants to use strings, one has to either import the string module use std::string::{String}or use a vector<u8> data type to store byte strings. We’ll talk about both options in this article.

aptos_std::debug::print serializes and displays a Move value on the console through a VM native function. If one wants to input byte strings to be serialized and printed to the console, one must thus convert the byte strings b”my string” to a human-readable format viautf8 encoding:

//using string module
module my_address::StringsDemo{
use std::debug;
use std::string::{String, utf8};

fun my_message():String {
let msg: String = utf8(b"This is the String I want to print to the screen.");
return msg
}


#[test]
fun testing(){
let msg = my_message();
debug::print(&msg);
}
}

Strings without using the String library

Alternatively one can print strings without using string::String by simply storing the string as a uint vector:

    #[test]
fun print_test_message(){
let msg:vector<u8> = b"This is my test message."; ;
debug::print(&msg); // prints the byte string
debug::print(&utf8(msg)); // prints the human readable string
}

Again, in the example of above, if you directly printed the u8 vector bytes string, the output is in bytes, i.e. not humanly readable (Figure below, 1st debug line). However, if you used the utf8-conversion, it’s in human readable format (Figure below, 2nd debug line).

Happy testing!

If this Aptos debugging intro was somewhat helpful to you, please return the favour by clapping down below or following Moncayo Labs. 👏 👏 👏

A little clap goes a long way!

Further Aptos Learning

References

For more information, please refer to the Move Book https://hack.movementlabs.xyz/week_1/Movement_standard_library.html

--

--

Moncayo Labs
Cryptocurrency Scripts

Active Supporter of the Aptos Move Movement | Web3 Move Development Tutorials