anowell / interpolate Goto Github PK
View Code? Open in Web Editor NEWVery simple Rust string interpolation
License: MIT License
Very simple Rust string interpolation
License: MIT License
I'm on 1.34.0-nightly and tried to do some trivial things with s!
but I couldn't get my code to compile.
I'm using interpolate 0.2.3. My next step was to run cargo test -p interpolate
in my project.
This is what I got:
$ cargo test -p interpolate
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
Running target/debug/deps/interpolate-c5fc2b5f74fbaa21
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Doc-tests interpolate
running 1 test
test src/lib.rs - (line 7) ... FAILED
failures:
---- src/lib.rs - (line 7) stdout ----
error[E0658]: procedural macros cannot be expanded to expressions (see issue #54727)
--> src/lib.rs:12:13
|
8 | let greet = s!("Hello, {name}");
| ^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(proc_macro_hygiene)] to the crate attributes to enable
error: expected `[`, found `fmt`
--> src/lib.rs:12:13
|
8 | let greet = s!("Hello, {name}");
| ^^^^^^^^^^^^^^^^^^^ expected `[`
error[E0658]: procedural macros cannot be expanded to expressions (see issue #54727)
--> src/lib.rs:13:11
|
9 | let sos = s!("HELP, {name.to_uppercase()}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(proc_macro_hygiene)] to the crate attributes to enable
error: expected `[`, found `fmt`
--> src/lib.rs:13:11
|
9 | let sos = s!("HELP, {name.to_uppercase()}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `[`
thread 'src/lib.rs - (line 7)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:351:13
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
failures:
src/lib.rs - (line 7)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--doc'
$ rustc --version
rustc 1.34.0-nightly (e1c6d0057 2019-02-22)
$ cargo --version
cargo 1.34.0-nightly (b33ce7fc9 2019-02-19)
I think this crate has potential and I want to use it a lot..
Btw, I also think there should be a shorter way to concatenate strings in Rust than "foo".to_string() + "bar"
, so with this crate it would be s!("foo") + "bar"
, right?
In swift, it allows exprs in the interpolated string like this:
https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html#ID292
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"
This would allow not just simple variable interpolations but actual expressions!
let s = s!("foo $(bar * 2) baz");
I want to use this crate in my yew frontend, but I'm getting:
error: cannot produce proc-macro for
interpolate v0.1.1
as the targetwasm32-unknown-unknown
does not support these crate types
:(
Any idea how to get it to work?
I found this issue: rust-lang/rust#52707
But it seems to be only about crates that depend on the proc-macro
crate (?)
How to escape the dollar sign in a string like
let price_usd = 9.99;
let price_str = s!("Total: $ $price_usd");
so that it becomes "Total: $ 9.99"
?
In JS:
var x = 1;
var a = "asd"
var s = `a ${a + ` x${x + 1} y`} b`;
console.log(s); // a asd x2 y b
Would this be the Rust equivalent?
let x = 1;
let a = "asd";
let s = s!("a ${a.to_string() + &s!(" x${x + 1} y")} b");
or alternatively:
let x = 1;
let a = s!("asd");
let s = s!("a ${a + &s!(" x${x + 1} y")} b");
?
If I run the example from the documentation in a new crate:
use interpolate::s;
fn main() {
let name = "Hercules";
let greet = s!("Hello, {name}");
}
I get the error error: expected one of `!` or `[`, found `fmt`
If I run cargo expand
, it shows that it expands to:
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use interpolate::s;
fn main() {
let name = "Hercules";
let greet = {
{
let res = ::alloc::fmt::format(());
res
}
};
}
Am I doing something wrong, or is it broken? I'd love to use this macro in my projects :)
Missing "
at the end of the string:
let greeting = s!("$name's favorite number is $fav_num);
interpolate = "0.2.1"
#[macro_use] extern crate interpolate;
Any idea why it's not working?
thread 'main' panicked at 'librustc_metadata\creader.rs:531: proc-macro crate not dylib', librustc\util\bug.rs:47:26
stack backtrace:
0: <std::sys::windows::args::Args as core::ops::drop::Drop>::drop
1: std::io::error::Error::get_ref
2: std::panicking::take_hook
3: std::panicking::take_hook
4: <rustc::ty::sty::Binder<rustc::ty::ProjectionPredicate<'tcx>> as rustc::ty::ToPredicate<'tcx>>::to_predicate
5: std::panicking::rust_panic_with_hook
6: <&'tcx rustc::hir::MacroDef as rustc_metadata::index_builder::DepGraphRead>::read
7: rustc_metadata::decoder::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc_metadata::decoder::DecodeContext<'a, 'tcx>>::read_u64
8: rustc_metadata::decoder::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc_metadata::decoder::DecodeContext<'a, 'tcx>>::read_u64
9: rustc_metadata::decoder::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc_metadata::decoder::DecodeContext<'a, 'tcx>>::read_u64
10: rustc_metadata::decoder::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc_metadata::decoder::DecodeContext<'a, 'tcx>>::read_u64
11: rustc_metadata::creader::CrateLoader::process_path_extern
12: rustc_metadata::creader::CrateLoader::process_path_extern
13: rustc_metadata::creader::CrateLoader::new
14: rustc_metadata::creader::CrateLoader::process_extern_crate
15: rustc_resolve::build_reduced_graph::<impl rustc_resolve::ToNameBinding<'a> for (&'a rustc_resolve::ModuleData<'a>, rustc::ty::Visibility, syntax_pos::span_encoding::Span, syntax_pos::hygiene::Mark)>::to_name_binding
16: <rustc_resolve::build_reduced_graph::BuildReducedGraphVisitor<'a, 'b, 'cl> as syntax::visit::Visitor<'a>>::visit_item
17: <rustc_resolve::check_unused::UnusedImportCheckVisitor<'a, 'b, 'cl> as syntax::visit::Visitor<'a>>::visit_use_tree
18: <rustc_resolve::build_reduced_graph::BuildReducedGraphVisitor<'a, 'b, 'cl> as syntax::visit::Visitor<'a>>::visit_item
19: <rustc_resolve::check_unused::UnusedImportCheckVisitor<'a, 'b, 'cl> as syntax::visit::Visitor<'a>>::visit_use_tree
20: rustc_resolve::macros::<impl syntax::ext::base::Resolver for rustc_resolve::Resolver<'a, 'crateloader>>::visit_ast_fragment_with_placeholders
21: syntax::ext::expand::MacroExpander::expand_crate
22: syntax::ext::expand::MacroExpander::expand_crate
23: syntax::ext::expand::MacroExpander::expand_crate
24: <rustc_driver::profile::trace::Query as core::fmt::Debug>::fmt
25: <rustc_driver::profile::trace::Query as core::fmt::Debug>::fmt
26: rustc_driver::driver::count_nodes
27: rustc_driver::driver::compile_input
28: rustc_driver::run_compiler
29: <env_logger::filter::inner::Filter as core::fmt::Display>::fmt
30: rustc_driver::run_compiler
31: <rustc_driver::profile::trace::Query as core::fmt::Debug>::fmt
32: rustc_driver::target_features::add_configuration
33: _rust_maybe_catch_panic
34: rustc_driver::profile::dump
35: rustc_driver::main
36: <unknown>
37: std::panicking::update_panic_count
38: _rust_maybe_catch_panic
39: std::rt::lang_start_internal
40: <unknown>
41: <unknown>
42: BaseThreadInitThunk
43: RtlUserThreadStart
query stack during panic:
end of query stack
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: rustc 1.30.0-nightly (90d36fb59 2018-09-13) running on x86_64-pc-windows-msvc
note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib
note: some of the compiler flags provided by cargo are hidden
How can I replace this call:
format!(
"transform-origin: {:.3}% center; transform: translateX({:.3}%) scaleX({:.3});",
percent,
100. - percent,
self.zoom
)
with a call to interpolate::s
?
This doesn't work:
s!(
"transform-origin: {percent:.3}% center; transform: translateX({100. - percent:.3}%) scaleX({self.zoom:.3});"
);
How to pass additional format args such as :.3
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.