Git Product home page Git Product logo

interpolate's Introduction

Interpolate

A simple form of Rust string interpolation, e.g., s!("Today is {date}").

Documentation

Crates.io

Usage

Note: interpolate currently requires some experimental functionality in nightly.

#![feature(proc_macro_hygiene)]
use interpolate::s;

let name = "Jane";
let fav_num = 32;
let greeting = s!("{name}'s favorite number is {fav_num}");

Escaping braces is accomplished similar to escaping other format strings in rust.

The literal characters { and } may be included in a string by preceding them with the same character. For example, the { character is escaped with {{ and the } character is escaped with }}.

Idea

The goal of interpolate is to provide basic string interpolation functionality with a very light-weight syntax.

It is not:

  • A full replacement for format!, println!, and related macros
  • Capable of non-trivial formatting of types
  • Anything that requires extensive documentation

I created this after a working on a CLI tools where I used format! a LOT. I really wanted something lighter weight like Scala's s"Today is $date", so I decided to experiment here, with the idea of possibly adding to the discussions around strings (like allowing literals to be used as String and custom string literals. I frequently find myself wondering if any of these ideas could have a more central role in rust:

  • println!("Hello {name}") to basically mean println!("Hello {name}", name=name)
  • let full_name = s"{first_name} {last_name}" instead of format!("{} {}", first_name, last_name)
  • let msg = s"Hello" instead of "Hello".to_string()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.