Menu
CoddyTech

Output and Comments

Lesson 2 of 48 in Coddy's Rust for Kotlin Developers course.

Kotlin permits a top-level main function, so an explicit enclosing class is unnecessary. Rust also begins at a free fn main. Both languages support line comments and block comments, and comment markers inside a quoted string remain data. Kotlin compiles this comparison for the JVM; the Rust program is a native executable.

Kotlin string concatenation proceeds left to right here: adding 2 to the initial string produces text before 3 is appended. A parenthesized numeric sum would behave differently. Rust formatting evaluates the supplied expression before inserting it into the placeholder. println! is a macro, with compile-time checks of the format and arguments. print! omits the newline, while format! returns an owned String. Neither commenting style nor changing println to print changes arithmetic grouping.

Kotlin: complete program

fun main() {
    // Addition starts after a string has already been produced.
    println("sum=" + 2 + 3)
}

Rust: complete program

fn main() {
    // Addition is evaluated before formatting.
    println!("sum={}", 2 + 3);
}

Parenthesize the Kotlin sum when numeric addition should happen before it meets the string.

challenge icon

Challenge

Easy

Build a two-line equipment badge. Input is one ASCII name, possibly empty, with no newline inside it. Required API: pub fn badge(name: &str) -> String returns DEVICE on the first line and the name inside square brackets on the second. Preserve all spaces in the name. For empty input the second line is []. For input pump, output is DEVICE followed by a newline and [pump]. Implement the public API in badge.rs. The supplied exercise.rs parses valid input and calls that API. Return the specified text without a trailing newline; the locked driver adds one newline. Do not print from the API. All input values satisfy the stated bounds.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, badge.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod badge;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
assert_eq!(badge::badge("valve #7"), "DEVICE\n[valve #7]");
assert_eq!(badge::badge(""), "DEVICE\n[]");
assert_eq!(badge::badge(" x "), "DEVICE\n[ x ]");
assert_eq!(badge::badge("valve #7"), "DEVICE\n[valve #7]");
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for Kotlin Developers

Practice on your own: Online Rust compiler