0Pricing
Kotlin Academy · Lesson

let and run: Transform and Scope

Apply let for null-safe transformations and run for local computations.

let and run

let uses it as the receiver; run uses this. Both return the lambda's last expression. Pick based on whether you prefer explicit references.

let Basics

let passes the receiver to the lambda as it. Useful for transforming a value or limiting a variable's scope.

fun main() {
    val length = "Kotlin".let { it.length }
    println(length) // 6
}

All lessons in this course

  1. apply and with: Configure and Return
  2. let and run: Transform and Scope
  3. also: Side Effects Without Changing the Receiver
  4. Choosing the Right Scope Function: Decision Guide
← Back to Kotlin Academy