This question already has an answer here:
Why is this match pattern unreachable when using non-literal patterns?
(1 answer)
Closed 5 years ago.
I am doing some simple stuff with rust... just touching some ground you know.
So I was playing with command line arguments, and I can't go past this:
use std::os::args;
fn main(){
let arg1 = args().get(1).to_str();
let help_command = "help";
if args().len() == 1 {
println!("No arguments.");
}
else if args().len() == 2 {
match arg1 {
help_command => println!("Do ..."),
_ => println!("no valid argument")
}
}
}
I can't compile... The error is:
main.rs:17:4: 17:5 error: unreachable pattern
main.rs:17 _ => println!("no valid argument")
^
error: aborting due to previous error
Also, I am using Rust 0.11.0-pre-nightly.
EDIT: Also, if I go with this approach:
match arg1 {
"help" => { /* ... / },
_ => { / ... */ },
}
It throws another error:
error: mismatched types: expected collections::string::String but found &'static str (expected struct collections::string::String but found &-ptr)
You can't use variables on Rust's match patterns. The code is being interpreted as binding whatever value is on arg1 as a new variable called help_command, and thus the catch-all pattern never matches.
You can use the literal string to match arg1:
match arg1 {
"help" => { /* ... */ },
_ => { /* ... */ },
}
Or use a guard:
match arg1 {
command if command == help_command => { /* ... */ },
_ => { /* ... */ }
}
If you are concerned about the type safety and/or repetition with using strings directly, you can parse the command into an enum:
enum Command {
HelpCommand,
DoStuffCommand
}
fn to_command(arg: &str) -> Option<Command> {
match arg {
"help" => Some(HelpCommand),
"do-stuff" => Some(DoStuffCommand),
_ => None,
}
}
Working example
Update (thanks #ChrisMorgan): It is also possible to use a static variable:
static HELP: &'static str = "help";
match arg1 {
HELP => { /* ... */ },
_ => { /* ... */ },
}
About the error reported in the question edit: Rust has two kinds of strings: &str (string slice) and String (owned string). The main difference is that the second is growable and can be moved. Refer to the links to understand the distinction better.
The error you are encountering is due to the fact that string literals ("foo") are of type &str, while std::os::args() is a Vec of String. The solution is simple: Use the .as_slice() method on the String to take slice out of it, and be able to compare it to the literal.
In code:
match arg1.as_slice() {
"help" => { /* ... */ },
_ => { /* ... */ },
}
Related
Let us consider a simple enum implementation with a static method that check whether a value has an associated value (the efficiency of the implementation is not to be regarded here):
enum Letter {
Alpha = -1,
A = 0,
B = 1,
C = 2,
}
impl Letter {
pub fn in_enum(value: isize) -> bool
{
match value {
-1 => true,
0 => true,
1 => true,
2 => true,
_ => false,
}
}
}
Now, let us write a macro for building enums with an equivalent in_enum method. The macro below was written with some guidance from the Serde guide for enum deserialization as numbers, in which matching for enum variant values also occurs.
macro_rules! my_enum {
($name:ident { $($variant:ident = $value:expr, )* }) => {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum $name {
$($variant = $value,)*
}
impl $name {
pub fn in_enum(value: isize) -> bool
{
match value {
$( $value => true, )*
_ => false,
}
}
}
}
}
my_enum!(Letter {
Alpha = -1,
A = 0,
B = 1,
C = 2,
});
Playground.
With version 1.18.0, the compiler won't accept the variant with a negative integer.
error: expected pattern, found `-1`
--> src/main.rs:13:24
|
13 | $( $value => true, )*
| ^^^^^^
This seems to happen regardless of how I write this pattern down in the macro, or whether I use i32 or isize for the value method parameter. Changing the fragment specifier of $value to pat is also out of the question: the compiler will refuse to build the enum, even without negative variant values.
error: expected expression, found `-1`
--> src/main.rs:5:26
|
5 | $($variant = $value,)*
| ^^^^^^
What's surprising about this is that it works without using macros, as well as when I discard the Alpha variant.
Why does this happen?
This is a bug in the compiler and is already fixed in the nightly version as of today (Jul 5, 2017), and released in stable version 1.20.0.
I want to create a macro that prints "Hello" a specified number of times. It's used like:
many_greetings!(3); // expands to three `println!("Hello");` statements
The naive way to create that macro is:
macro_rules! many_greetings {
($times:expr) => {{
println!("Hello");
many_greetings!($times - 1);
}};
(0) => ();
}
However, this doesn't work because the compiler does not evaluate expressions; $times - 1 isn't calculated, but fed as a new expression into the macro.
While the ordinary macro system does not enable you to repeat the macro expansion many times, there is no problem with using a for loop in the macro:
macro_rules! many_greetings {
($times:expr) => {{
for _ in 0..$times {
println!("Hello");
}
}};
}
If you really need to repeat the macro, you have to look into procedural macros/compiler plugins (which as of 1.4 are unstable, and a bit harder to write).
Edit: There are probably better ways of implementing this, but I've spent long enough on this for today, so here goes. repeat!, a macro that actually duplicates a block of code a number of times:
main.rs
#![feature(plugin)]
#![plugin(repeat)]
fn main() {
let mut n = 0;
repeat!{ 4 {
println!("hello {}", n);
n += 1;
}};
}
lib.rs
#![feature(plugin_registrar, rustc_private)]
extern crate syntax;
extern crate rustc;
use syntax::codemap::Span;
use syntax::ast::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, MacEager, DummyResult};
use rustc::plugin::Registry;
use syntax::util::small_vector::SmallVector;
use syntax::ast::Lit_;
use std::error::Error;
fn expand_repeat(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'static> {
let mut parser = cx.new_parser_from_tts(tts);
let times = match parser.parse_lit() {
Ok(lit) => match lit.node {
Lit_::LitInt(n, _) => n,
_ => {
cx.span_err(lit.span, "Expected literal integer");
return DummyResult::any(sp);
}
},
Err(e) => {
cx.span_err(sp, e.description());
return DummyResult::any(sp);
}
};
let res = parser.parse_block();
match res {
Ok(block) => {
let mut stmts = SmallVector::many(block.stmts.clone());
for _ in 1..times {
let rep_stmts = SmallVector::many(block.stmts.clone());
stmts.push_all(rep_stmts);
}
MacEager::stmts(stmts)
}
Err(e) => {
cx.span_err(sp, e.description());
DummyResult::any(sp)
}
}
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("repeat", expand_repeat);
}
added to Cargo.toml
[lib]
name = "repeat"
plugin = true
Note that if we really don't want to do looping, but expanding at compile-time, we have to do things like requiring literal numbers. After all, we are not able to evaluate variables and function calls that reference other parts of the program at compile time.
As the other answers already said: no, you can't count like this with declarative macros (macro_rules!).
But you can implement the many_greetings! example as a procedural macro. procedural macros were stabilized a while ago, so the definition works on stable. However, we can't yet expand macros into statements on stable -- that's what the #![feature(proc_macro_hygiene)] is for.
This looks like a lot of code, but most code is just error handling, so it's not that complicated!
examples/main.rs
#![feature(proc_macro_hygiene)]
use count_proc_macro::many_greetings;
fn main() {
many_greetings!(3);
}
Cargo.toml
[package]
name = "count-proc-macro"
version = "0.1.0"
authors = ["me"]
edition = "2018"
[lib]
proc-macro = true
[dependencies]
quote = "0.6"
src/lib.rs
extern crate proc_macro;
use std::iter;
use proc_macro::{Span, TokenStream, TokenTree};
use quote::{quote, quote_spanned};
/// Expands into multiple `println!("Hello");` statements. E.g.
/// `many_greetings!(3);` will expand into three `println`s.
#[proc_macro]
pub fn many_greetings(input: TokenStream) -> TokenStream {
let tokens = input.into_iter().collect::<Vec<_>>();
// Make sure at least one token is provided.
if tokens.is_empty() {
return err(Span::call_site(), "expected integer, found no input");
}
// Make sure we don't have too many tokens.
if tokens.len() > 1 {
return err(tokens[1].span(), "unexpected second token");
}
// Get the number from our token.
let count = match &tokens[0] {
TokenTree::Literal(lit) => {
// Unfortunately, `Literal` doesn't have nice methods right now, so
// the easiest way for us to get an integer out of it is to convert
// it into string and parse it again.
if let Ok(count) = lit.to_string().parse::<usize>() {
count
} else {
let msg = format!("expected unsigned integer, found `{}`", lit);
return err(lit.span(), msg);
}
}
other => {
let msg = format!("expected integer literal, found `{}`", other);
return err(other.span(), msg);
}
};
// Return multiple `println` statements.
iter::repeat(quote! { println!("Hello"); })
.map(TokenStream::from)
.take(count)
.collect()
}
/// Report an error with the given `span` and message.
fn err(span: Span, msg: impl Into<String>) -> TokenStream {
let msg = msg.into();
quote_spanned!(span.into()=> {
compile_error!(#msg);
}).into()
}
Running cargo run --example main prints three "Hello"s.
For those looking for a way to do this, there is also the seq_macro crate.
It is fairly easy to use and works out of the box with stable Rust.
use seq_macro::seq;
macro_rules! many_greetings {
($times:literal) => {
seq!{ N in 0..$times {
println!("Hello");
}}
};
}
fn main() {
many_greetings!(3);
many_greetings!(12);
}
As far as I know, no. The macro language is based on pattern matching and variable substitution, and only evaluates macros.
Now, you can implement counting with evaluation: it just is boring... see the playpen
macro_rules! many_greetings {
(3) => {{
println!("Hello");
many_greetings!(2);
}};
(2) => {{
println!("Hello");
many_greetings!(1);
}};
(1) => {{
println!("Hello");
many_greetings!(0);
}};
(0) => ();
}
Based on this, I am pretty sure one could invent a set of macro to "count" and invoke various operations at each step (with the count).
Is it possible to write a macro that generates a function where the number of arguments to this function to be a determined by the macro? For instance, I'd like to write something to make using prepared statements in the Cassandra driver easier.
let prepared = prepare!(session, "insert into blah (id, name, reading ) values (?, ?, ?)", int, string, float);
let stmt = prepared(1, "test".to_string(), 3.1);
session.execute(stmt);
prepare! would need to generate something like (unwrap only here for brevity):
fn some_func(arg1, arg2, arg3) -> Statement {
let mut statement = Statement::new("insert into blah (id, name, reading ) values (?, ?, ?)", 3);
statement.bind_int(0, arg1).unwrap()
.bind_string(1, arg2).unwrap()
.bind_float(2, arg3).unwrap()
}
Two hard things in Rust macros: counting and unique identifers. You have both. Then again, I'm the one writing the answer, so I suppose it's my problem now. At least you didn't ask about parsing the string (which is outright impossible without compiler plugins).
Another impossible thing is mapping types to different methods. You just can't. Instead, I'm going to assume the existence of a helper trait that does this mapping.
Also, Rust doesn't have int, string, or float. I assume you mean i32, String, and f32.
Finally, the way you've written the invocation and expansion don't really gel. I don't see why session is involved; it's not used in the expansion. So I'm going to take the liberty of just pretending you don't need it; if you do, you'll have to hack it back in.
So, with that, here's what I came up with.
// Some dummy types so the following will type-check.
struct Statement;
impl Statement {
fn new(stmt: &str, args: usize) -> Self { Statement }
fn bind_int(self, pos: usize, value: i32) -> Result<Self, ()> { Ok(self) }
fn bind_float(self, pos: usize, value: f32) -> Result<Self, ()> { Ok(self) }
fn bind_string(self, pos: usize, value: String) -> Result<Self, ()> { Ok(self) }
}
struct Session;
impl Session {
fn execute(&self, stmt: Statement) {}
}
// The supporting `BindArgument` trait.
trait BindArgument {
fn bind(stmt: Statement, pos: usize, value: Self) -> Statement;
}
impl BindArgument for i32 {
fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
stmt.bind_int(pos, value).unwrap()
}
}
impl BindArgument for f32 {
fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
stmt.bind_float(pos, value).unwrap()
}
}
impl BindArgument for String {
fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
stmt.bind_string(pos, value).unwrap()
}
}
// The macro itself.
macro_rules! prepare {
// These three are taken straight from
// https://danielkeep.github.io/tlborm/book/
(#as_expr $e:expr) => {$e};
(#count_tts $($tts:tt)*) => {
<[()]>::len(&[$(prepare!(#replace_tt $tts ())),*])
};
(#replace_tt $_tt:tt $e:expr) => {$e};
// This is how we bind *one* argument.
(#bind_arg $stmt:expr, $args:expr, $pos:tt, $t:ty) => {
prepare!(#as_expr <$t as BindArgument>::bind($stmt, $pos, $args.$pos))
};
// This is how we bind *N* arguments. Note that because you can't do
// arithmetic in macros, we have to spell out every supported integer.
// This could *maybe* be factored down with some more work, but that
// can be homework. ;)
(#bind_args $stmt:expr, $args:expr, 0, $next:ty, $($tys:ty,)*) => {
prepare!(#bind_args prepare!(#bind_arg $stmt, $args, 0, $next), $args, 1, $($tys,)*)
};
(#bind_args $stmt:expr, $args:expr, 1, $next:ty, $($tys:ty,)*) => {
prepare!(#bind_args prepare!(#bind_arg $stmt, $args, 1, $next), $args, 2, $($tys,)*)
};
(#bind_args $stmt:expr, $args:expr, 2, $next:ty, $($tys:ty,)*) => {
prepare!(#bind_args prepare!(#bind_arg $stmt, $args, 2, $next), $args, 3, $($tys,)*)
};
(#bind_args $stmt:expr, $_args:expr, $_pos:tt,) => {
$stmt
};
// Finally, the entry point of the macro.
($stmt:expr, $($tys:ty),* $(,)*) => {
{
// I cheated: rather than face the horror of trying to *also* do
// unique identifiers, I just shoved the arguments into a tuple, so
// that I could just re-use the position.
fn prepared_statement(args: ($($tys,)*)) -> Statement {
let statement = Statement::new(
$stmt,
prepare!(#count_tts $(($tys))*));
prepare!(#bind_args statement, args, 0, $($tys,)*)
}
prepared_statement
}
};
}
fn main() {
let session = Session;
let prepared = prepare!(
r#"insert into blah (id, name, reading ) values (?, ?, ?)"#,
i32, String, f32);
// Don't use .to_string() for &str -> String; it's horribly inefficient.
let stmt = prepared((1, "test".to_owned(), 3.1));
session.execute(stmt);
}
And here's what the main function expands to, to give you a frame of reference:
fn main() {
let session = Session;
let prepared = {
fn prepared_statement(args: (i32, String, f32)) -> Statement {
let statement = Statement::new(
r#"insert into blah (id, name, reading ) values (?, ?, ?)"#,
<[()]>::len(&[(), (), ()]));
<f32 as BindArgument>::bind(
<String as BindArgument>::bind(
<i32 as BindArgument>::bind(
statement, 0, args.0),
1, args.1),
2, args.2)
}
prepared_statement
};
// Don't use .to_string() for &str -> String; it's horribly inefficient.
let stmt = prepared((1, "test".to_owned(), 3.1));
session.execute(stmt);
}
I need to get index of macro repetition element to write next code:
struct A {
data: [i32; 3]
}
macro_rules! tst {
( $( $n:ident ),* ) => {
impl A {
$(
fn $n(self) -> i32 {
self.data[?] // here I need the index
}
),*
}
}
}
I know one way to do it: just tell user to write index by hands:
( $( $i:ident => $n:ident ),* )
But is there a more elegant way which does not require user's action?
The easiest way is to use recursion, like so:
struct A {
data: [i32; 3]
}
macro_rules! tst {
(#step $_idx:expr,) => {};
(#step $idx:expr, $head:ident, $($tail:ident,)*) => {
impl A {
fn $head(&self) -> i32 {
self.data[$idx]
}
}
tst!(#step $idx + 1usize, $($tail,)*);
};
($($n:ident),*) => {
tst!(#step 0usize, $($n,)*);
}
}
tst!(one, two, three);
fn main() {
let a = A { data: [10, 20, 30] };
println!("{:?}", (a.one(), a.two(), a.three()));
}
Note that I changed the method to take &self instead of self, since it made writing the example in the main function easier. :)
Each step in the recursion just adds 1 to the index. It is a good idea to use "typed" integer literals to avoid compilation slowdown due to lots and lots of integer inference.
I'm getting frustrated trying to convert a small part of the Golang templating language to Scala.
Below are the key parts of the lex.go source code: https://github.com/golang/go/blob/master/src/text/template/parse/lex.go
The tests are here: https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
Basically this "class" takes a string and returns an Array of "itemType". In the template string, the start and end of special tokens is using curly braces {{ and }}.
For for example:
"{{for}}"
returns an array of 4 items:
item{itemLeftDelim, 0, "{{" } // scala case class would be Item(ItemLeftDelim, 0, "")
item{itemIdentifier, 0, "for"}
item{itemRightDelim, 0, "}}"}
item{itemEOF, 0, ""}
The actual call would look like:
l := lex("for", `{{for}}`, "{{", "}}") // you pass in the start and end delimeters {{ and }}
for {
item := l.nextItem()
items = append(items, item)
if item.typ == itemEOF || item.typ == itemError {
break
}
}
return
The key parts of the source code are below:
// itemType identifies the type of lex items.
type itemType int
const (
itemError itemType = iota // error occurred; value is text of error
itemEOF
itemLeftDelim // left action delimiter
// .............. skipped
)
const (
leftDelim = "{{"
rightDelim = "}}"
leftComment = "/*"
rightComment = "*/"
)
// item represents a token or text string returned from the scanner.
type item struct {
typ itemType // The type of this item.
pos Pos // The starting position, in bytes, of this item in the input string.
val string // The value of this item.
}
// stateFn represents the state of the scanner as a function that returns the next state.
type stateFn func(*lexer) stateFn
// lexer holds the state of the scanner.
type lexer struct {
name string // the name of the input; used only for error reports
input string // the string being scanned
leftDelim string // start of action
rightDelim string // end of action
state stateFn // the next lexing function to enter
pos Pos // current position in the input
start Pos // start position of this item
width Pos // width of last rune read from input
lastPos Pos // position of most recent item returned by nextItem
items chan item // channel of scanned items
parenDepth int // nesting depth of ( ) exprs
}
// lex creates a new scanner for the input string.
func lex(name, input, left, right string) *lexer {
if left == "" {
left = leftDelim
}
if right == "" {
right = rightDelim
}
l := &lexer{
name: name,
input: input,
leftDelim: left,
rightDelim: right,
items: make(chan item),
}
go l.run()
return l
}
// run runs the state machine for the lexer.
func (l *lexer) run() {
for l.state = lexText; l.state != nil; {
l.state = l.state(l)
}
}
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
item := <-l.items
l.lastPos = item.pos
return item
}
// emit passes an item back to the client.
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.start, l.input[l.start:l.pos]}
l.start = l.pos
}
// lexText scans until an opening action delimiter, "{{".
func lexText(l *lexer) stateFn {
for {
if strings.HasPrefix(l.input[l.pos:], l.leftDelim) {
if l.pos > l.start {
l.emit(itemText)
}
return lexLeftDelim
}
if l.next() == eof {
break
}
}
// Correctly reached EOF.
if l.pos > l.start {
l.emit(itemText)
}
l.emit(itemEOF)
return nil
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if int(l.pos) >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = Pos(w)
l.pos += l.width
return r
}
// lexLeftDelim scans the left delimiter, which is known to be present.
func lexLeftDelim(l *lexer) stateFn {
l.pos += Pos(len(l.leftDelim))
if strings.HasPrefix(l.input[l.pos:], leftComment) {
return lexComment
}
l.emit(itemLeftDelim)
l.parenDepth = 0
return lexInsideAction
}
// lexRightDelim scans the right delimiter, which is known to be present.
func lexRightDelim(l *lexer) stateFn {
l.pos += Pos(len(l.rightDelim))
l.emit(itemRightDelim)
return lexText
}
// there are more stateFn
So I was able to write the item and itemType:
case class Item(typ: ItemType, pos: Int, v: String)
sealed trait ItemType
case object ItemError extends ItemType
case object ItemEOF extends ItemType
case object ItemLeftDelim extends ItemType
...
..
.
The stateFn and Lex definitions:
trait StateFn extends (Lexer => StateFn) {
}
I'm basically really stuck on the main parts here. So things seem to be kicked of like this:
A Lex is created, then "go l.run()" is called.
Run is a loop, which keeps looping until EOF or an error is found.
The loop initializes with lexText, which scans until it finds an {{, and then it sends a message to a channel with all the preceding text of type 'itemText', passing it an 'item'. It then returns the function lexLeftDelim. lexLeftDelim does the same sort of thing, it sends a message 'item' of type itemLeftDelim.
It keeps parsing the string until it reaches EOF basically.
I can't think in scala that well, but I know I can use an Actor here to pass it a message 'Item'.
The part of returning a function, I asked I got some good ideas here: How to model recursive function types?
Even after this, I am really frustrated and I can seem to glue these concepts together.
I'm not looking for someone to implement the entire thing for me, but if someone could write just enough code to parse a simple string like "{{}}" that would be awesome. And if they could explain why they did a certain design that would be great.
I created a case class for Lex:
case class Lex(
name: String,
input: String,
leftDelim: String,
rightDelim: String,
state: StateFn,
var pos: Int = 0,
var start: Int = 0,
var width: Int = 0,
var lastPos: Int = 0,
var parenDepth: Int = 0
) {
def next(): Option[String] = {
if (this.pos >= this.input.length) {
this.width = 0
return None
}
this.width = 1
val nextChar = this.input.drop(this.pos).take(1)
this.pos += 1
Some(nextChar)
}
}
The first stateFn is LexText and so far I have:
object LexText extends StateFn {
def apply(l: Lexer) = {
while {
if (l.input.startsWith(l.leftDelim)) {
if (l.pos > l.start) {
// ????????? emit itemText using an actor?
}
return LexLeftDelim
}
if (l.next() == None) {
break
}
}
if(l.pos > l.start) {
// emit itemText
}
// emit EOF
return None // ?? nil? how can I support an Option[StateFn]
}
}
I need guidance on getting the Actor's setup, along with the main run loop:
func (l *lexer) run() {
for l.state = lexText; l.state != nil; {
l.state = l.state(l)
}
}
This is an interesting problem domain that I tried to tackle using Scala, and so far I am a bit confused hoping some else finds it interesting and can work with what little I have so far and provide some code and critique if I am doing it correctly or not.
I know deep down I shouldn't be mutating, but I'm still on the first few pages of the functional book :)
If you translate the go code literally into Scala, you'll get very unidiomatic piece of code. You'll probably get much more maintainable (and shorter!) Scala version by using parser combinators. There are plenty of resources about them on the internet.
import scala.util.parsing.combinator._
sealed trait ItemType
case object LeftDelim extends ItemType
case object RightDelim extends ItemType
case object Identifier extends ItemType
case class Item(ty: ItemType, token: String)
object ItemParser extends RegexParsers {
def left: Parser[Item] = """\{\{""".r ^^ { _ => Item(LeftDelim, "{{") }
def right: Parser[Item] = """\}\}""".r ^^ { _ => Item(RightDelim, "}}") }
def ident: Parser[Item] = """[a-z]+""".r ^^ { x => Item(Identifier, x) }
def item: Parser[Item] = left | right | ident
def items: Parser[List[Item]] = rep(item)
}
// ItemParser.parse(ItemParser.items, "{{foo}}")
// res5: ItemParser.ParseResult[List[Item]] =
// [1.8] parsed: List(Item(LeftDelim,{{), Item(Identifier,foo), Item(RightDelim,}}))
Adding whitespace skipping, or configurable left and right delimiters is trivial.