Skip to content

Instantly share code, notes, and snippets.

@asdfsx
Last active November 11, 2016 04:05
Show Gist options
  • Save asdfsx/6d9504d59d56392327fb557192662d11 to your computer and use it in GitHub Desktop.
Save asdfsx/6d9504d59d56392327fb557192662d11 to your computer and use it in GitHub Desktop.
use std::env;
fn main() {
let mut tomlfile = "";
for argument in env::args() {
let tmp = &*argument;
if tmp.contains("toml=") {
println!("{}", &tmp[5 .. tmp.len()]);
tomlfile = &tmp[5 .. tmp.len()];
}
}
println!("{}", tomlfile);
}
@asdfsx
Copy link
Author

asdfsx commented Nov 11, 2016

   Compiling toml_example v0.1.0 (file:///Users/sunxia/git_project/ruststudy/toml_example)
error: `argument` does not live long enough
 --> src/main.rs:8:21
  |
8 |         let tmp = &*argument;
  |                     ^^^^^^^^
  |
note: reference must be valid for the block suffix following statement 1 at 5:26...
 --> src/main.rs:5:27
  |
5 |     let mut tomlfile = "";
  |                           ^
note: ...but borrowed value is only valid for the for at 7:4
 --> src/main.rs:7:5
  |
7 |     for argument in env::args() {
  |     ^

error: aborting due to previous error

error: Could not compile `toml_example`.

To learn more, run the command again with --verbose.

@asdfsx
Copy link
Author

asdfsx commented Nov 11, 2016

new code:

use std::env;

fn main() {
    let mut tomlfile = String::new();

    for argument in env::args() {
        let tmp = &*argument;
        if tmp.contains("toml=") {
            tomlfile = (&tmp[5 .. tmp.len()]).to_string();
        }
    }

    println!("{}", tomlfile);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment