Skip to content

Instantly share code, notes, and snippets.

@hjr3
Created October 27, 2016 04:15
Show Gist options
  • Save hjr3/22b8b0a2f17e8747dfbaafd48bfa8ee0 to your computer and use it in GitHub Desktop.
Save hjr3/22b8b0a2f17e8747dfbaafd48bfa8ee0 to your computer and use it in GitHub Desktop.
Rust Belt Rust: Building Better Function Interfaces Workshop - Exercise 1
use std::mem;
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct NameString {
inner: String,
}
impl NameString {
pub fn from_str(s: &str) -> NameString {
NameString {
inner: s.to_string()
}
}
pub fn as_str(&self) -> &str {
self.inner.as_str()
}
// TODO new method to convert a `NameString` into a mutable `str` reference
}
#[derive(Eq, PartialEq, Debug)]
pub struct NameStr {
inner: str,
}
impl NameStr {
pub fn new(name: &str) -> &NameStr {
unsafe { mem::transmute(name) }
}
// TODO new method to convert a `NameStr` to a `String` type
}
pub struct Courses {
inner: Vec<String>,
}
impl Courses {
pub fn new() -> Courses {
Courses {
inner: Vec::new(),
}
}
// TODO: modify method to add courses that are of type `String` or `&str`
pub fn add_courses(&self) {
unimplemented!();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exercise1a() {
assert!(false, "Convert a `NameString` into a mutable `str` reference.");
}
#[test]
fn test_exercise1b() {
assert!(false, "Convert a `NameStr` to a `String` type.");
}
#[test]
fn test_courses_add_course() {
assert!(false, "Create a `Courses` type that can accept `String` or `&str`.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment