Update on rust-toys

Posted on April 12, 2015 by Tommy McGuire
Labels: rust

With the release of rust 1.0.0-beta, I've received a ping about updating my code from 0.11. Unfortunately, I hit the following:


let line : String = line.unwrap();
let line : &str = line.as_str().trim();

The variable line coming in is a io::Result (Is it me or are there two Result types in the standard library, std::result::Result and std::io::Result?); unwrap() returns a String (or blows up), which I then need to convert to a string slice (&str) in order to trim whitespace, using something like String::as_str or Str::as_slice.

Unfortunately, that code fails to compile with:


src/bin/mk_anadict.rs:18:32: 18:40 error: use of unstable library feature 'convert': waiting on RFC revision
src/bin/mk_anadict.rs:18 let line : &str = line.as_str().trim();
^~~~~~~~
src/bin/mk_anadict.rs:18:40: 18:40 help: add #![feature(convert)] to the crate attributes to enable

And the convert feature is not allowed in the beta release channel. (There was a fix for the error message suggestion, to prevent the compiler from showing "add #![feature..." when that won't work, but it didn't make it into the beta release.

I'm going to bail on this for the moment, until the standard library settles down a bit.

PS. Wait, what?

I realized I hadn't tried one thing: calling trim() on the String itself.


let line : String = line.unwrap();
let line : &str = line.trim();

It works. I don't know how.

Later. Hah. Because there is a Str implementation for String. I think. As I recall, there wasn't previously, which was why I needed to get the str explicitly.

active directory applied formal logic ashurbanipal authentication books c c++ comics conference continuations coq data structure digital humanities Dijkstra eclipse virgo electronics emacs goodreads haskell http java job Knuth ldap link linux lisp math naming nimrod notation OpenAM osgi parsing pony programming language protocols python quote R random REST ruby rust SAML scala scheme shell software development system administration theory tip toy problems unix vmware yeti
Member of The Internet Defense League
Site proudly generated by Hakyll.