Giter VIP home page Giter VIP logo

learn-rust's Introduction

An interactive guide to learning Rust!

  • It requires you to look at the docs!
  • It requires you to search the web.
  • It requires you to ask for help on IRC.

Requirements:

Tools

cargo is Rust's package manager and part of the Rust instalation. You can use it to access public packages on https://crates.io, create new packages or format your code.

Steps:

The goal is to fix/enhance the piece of code in each branch. Start at branch step1 (default branch after checking out).

  • Try to get the program to compile at each step. You can use cargo run, cargo watch or cargo watch -x run to achieve that feat.
  • Once the program compiles move onto the next branch step1 -> step1.1 -> step2 -> step2.1 -> step3.
  • If you get stuck, try the docs, web, or IRC.

Answers

Yes the answers can be found in the branch answer. Try not to cheat; you wont learn! Asking on IRC is preferable to cheating.

learn-rust's People

Contributors

toidiu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-rust's Issues

cargo run fails (with teach-rust v0.2.0)

cargo run

on branch step1 fails with:

> cargo run
   Compiling teach-rust v0.2.0 (file:///home/rawland/src/learn-rust)
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
  --> src/main.rs:8:23
   |
8  |     let xml: String = get_mta_status();
   |                       ^^^^^^^^^^^^^^^^ expected 1 parameter
...
11 | fn get_mta_status(client: Client) -> String {
   | ------------------------------------------- defined here

error: aborting due to previous error

error: Could not compile `teach-rust`.

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

Using rust 1.25.0 on LinuxMint 18.3 here.

Step3 unclear, possible successful compilation without clearly stated completions criteria

extern crate reqwest;
extern crate xml;

use self::xml::reader::{EventReader, XmlEvent};
use reqwest::Client;
use reqwest::Response;

fn main() {
    let client = Client::new();

    let xml: String = get_mta_status(&client);

    // passing `String` wont work because String doesnt implement std::io::Read
    // parse_xml(xml);
    let readable: &[u8] = xml.as_bytes();
    let lines = parse_xml(readable);
    for line in lines.iter() {
        println!("{:?}", line);
    };
}

fn get_mta_status(client: &Client) -> String {
    let mut resp: Response = client
        .get("http://web.mta.info/status/serviceStatus.txt")
        .send()
        .unwrap();
    let body: String = resp.text().unwrap();

    body
}

#[derive(Debug)]
struct Line {
    name: String,
    status: String,
}

impl Line {
    fn empty() -> Line {
        Line {
            name: "".into(),
            status: "".into(),
        }
    }

    // fn adt
}

/// Because we are using a XML event streaming
/// library we need to maintain state of which
/// tag we are processing.
enum XmlTag {
    TimeStamp,
    LineName,
    LineStatus,
    Ignore,
}

fn parse_xml<T>(readable: T) -> Vec<Line>
where
    T: std::io::Read,
{
    let reader = EventReader::new(readable);
    // This will hold the current tag we are processing.
    // We set it to a default value or `Ignore`.
    let mut xml_tag: XmlTag = XmlTag::Ignore;

    //TODO Populate the `Vec` by `push`ing `temp_line` elements
    // into it.
    let lines = Vec::new();

    let temp_line = Line::empty();

    for e in reader {

        match e {
            Ok(XmlEvent::StartElement { name, .. }) => {
                let ref_name: &str = name.local_name.as_ref();
                match ref_name {
                    "line" => {
                        //FIXME
                        // temp_line = Line::empty();
                        // temp_line 
                    }

                    "timestamp" => {
                        xml_tag = XmlTag::TimeStamp;
                        print!("{}: ", name);
                    }

                    "name" => {
                        xml_tag = XmlTag::LineName;
                    }

                    "status" => {
                        xml_tag = XmlTag::LineStatus;
                    }

                    _ => {
                        xml_tag = XmlTag::Ignore;
                    }
                }
            }

            Ok(XmlEvent::Characters(txt)) => match xml_tag {
                XmlTag::TimeStamp => println!("{}", txt),

                XmlTag::LineName => {
                }

                XmlTag::LineStatus => {
                }

                _ => (),
            },

            Ok(XmlEvent::EndElement { name }) => {
                let ref_name: &str = name.local_name.as_ref();
                match ref_name {
                    "line" => {
                        //FIXME
                    }

                    // we only care about subway
                    "subway" => break,

                    _ => (),
                }
            }

            Err(e) => {
                println!("Error: {}", e);
                break;
            }

            _ => (),
        }
    }

    lines
}

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.