Output and Comments
Lesson 2 of 48 in Coddy's Rust for C# Developers course.
A C# console application calls Console.WriteLine; Rust calls the println! macro. The exclamation mark belongs to the macro name. Both examples print a literal containing braces, but their formatting rules differ: the one-argument C# overload treats braces as ordinary text, while Rust's format string needs doubled braces. Rust formatting arguments follow the format string. // starts a line comment in both languages and produces no output. The C# program has a static entry method inside a class; Rust has a crate-level main function. Neither example reads input. print! leaves the current line open; println! terminates it. Keep the observable output separate from the surrounding program structure: the wrapper is needed to execute the example, but only the print calls produce the two lines. The task uses a returned string instead of console printing.
Complete C# program:
using System;
class Program {
static void Main() {
// A literal with braces.
Console.WriteLine("{ready}");
Console.WriteLine("{0}:{1}", "port", 3);
}
}Complete Rust program:
fn main() {
// A literal with braces.
println!("{{ready}}");
println!("{}:{}", "port", 3);
}Challenge
EasyBuild 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));
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Rust for C# Developers
1First Rust Programs
Run Rust ModulesOutput and CommentsModules and VisibilityBindings and TypesNumbers and CastsRecap: Storage Invoice4Ownership and Borrowing
Ownership and MovesShared BorrowsMutable BorrowsLifetimes and ViewsBorrowing Recap3Functions and Sequences
Functions and ReturnsArrays and SlicesVectors and IterationRecap: Record ReadingsPractice on your own: Online Rust compiler