DEV Community

V Sai Harsha
V Sai Harsha

Posted on • Updated on

Mojo cheat sheet

Today, Mojo SDK is officially launched by Modular Team. So, Today I going to make a cheat sheet for you!

Declaring a Mutable Variable

var x = 1
Enter fullscreen mode Exit fullscreen mode

Explicitly Typed Variable

var x: Int = 1
Enter fullscreen mode Exit fullscreen mode

Declaring an Immutable Variable

let x = 1
Enter fullscreen mode Exit fullscreen mode

Functions

fn greet():
    print("🔥🔥")
Enter fullscreen mode Exit fullscreen mode

Structs

struct MyPair:
    var first: Int
    var second: Int

    fn __init__(inout self, first: Int, second: Int):
        self.first = first
        self.second = second

    fn dump(self):
        print(self.first, self.second)
Enter fullscreen mode Exit fullscreen mode

Integrate with Python

from python import Python

let np = Python.import_module("numpy")

ar = np.arange(15).reshape(3, 5)
print(ar)
print(ar.shape)
Enter fullscreen mode Exit fullscreen mode

This cheat sheet covers the basics of Mojo and I do know this is short but Mojo has a lot of complex concepts related to memory management, copyinit, ownership and borrow checking. As of today, Mojo is early in development and released recently. So, I like this Mutli-threaded language and its memory management is similar to Rust. It does not many features of Python like anynomynous functions with lambda.

Top comments (0)