Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"slug": "rna-transcription",
"difficulty": 4,
"topics": [
"Result",
"match",
"struct",
"str vs string"
Expand Down
2 changes: 1 addition & 1 deletion exercises/rna-transcription/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion exercises/rna-transcription/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[package]
name = "rna-transcription"
version = "0.0.0"
version = "1.0.0"
22 changes: 13 additions & 9 deletions exercises/rna-transcription/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub struct DeoxyribonucleicAcid {
nucleotides: String
}

fn transcribe_dna_rna(c: char) -> char {
fn transcribe_dna_rna(c: char) -> Option<char> {
match c {
'C' => 'G',
'G' => 'C',
'A' => 'U',
'T' => 'A',
_ => c
'C' => Some('G'),
'G' => Some('C'),
'A' => Some('U'),
'T' => Some('A'),
_ => None
}
}

Expand All @@ -29,8 +29,12 @@ impl DeoxyribonucleicAcid {
DeoxyribonucleicAcid { nucleotides: nucleotides.to_string() }
}

pub fn to_rna(&self) -> RibonucleicAcid {
let rna_nucleotides = self.nucleotides.chars().map(transcribe_dna_rna).collect();
RibonucleicAcid { nucleotides: rna_nucleotides }
pub fn to_rna(&self) -> Result<RibonucleicAcid, ()> {
let rna_nucleotides: String = self.nucleotides.chars().filter_map(transcribe_dna_rna).collect();
if rna_nucleotides.len() == self.nucleotides.len() {
Ok(RibonucleicAcid { nucleotides: rna_nucleotides })
} else {
Err(())
}
}
}
28 changes: 23 additions & 5 deletions exercises/rna-transcription/tests/rna-transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,47 @@ fn test_acid_equals_acid() {
#[test]
#[ignore]
fn test_transcribes_cytosine_guanine() {
assert_eq!(dna::RibonucleicAcid::new("G"), dna::DeoxyribonucleicAcid::new("C").to_rna());
assert_eq!(Ok(dna::RibonucleicAcid::new("G")), dna::DeoxyribonucleicAcid::new("C").to_rna());
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is a different approach from, say, #238 which used unwrap. Note that https://github.com/exercism/xrust/blob/master/exercises/nucleotide-codons/tests/codons.rs does use assert_eq with Ok though. I think I like this better. The difference is what happens when I return an Err when I should return Ok. With assert_eq!(Ok(...), ...):

---- test_transcribes_cytosine_guanine stdout ----
	thread 'test_transcribes_cytosine_guanine' panicked at 'assertion failed: `(left == right)` (left: `Ok(RibonucleicAcid { nucleotides: "G" })`, right: `Err(())`)', tests/rna-transcription.rs:12

With assert_eq!(..., ....unwrap());

---- test_transcribes_cytosine_guanine stdout ----
	thread 'test_transcribes_cytosine_guanine' panicked at 'called `Result::unwrap()` on an `Err` value: ()', src/libcore/result.rs:859

So why don't I go ahead and say now that I like assert_eq(Ok(...), ...) better since it shows me both sides.

}

#[test]
#[ignore]
fn test_transcribes_guanine_cytosine() {
assert_eq!(dna::RibonucleicAcid::new("C"), dna::DeoxyribonucleicAcid::new("G").to_rna());
assert_eq!(Ok(dna::RibonucleicAcid::new("C")), dna::DeoxyribonucleicAcid::new("G").to_rna());
}

#[test]
#[ignore]
fn test_transcribes_adenine_uracil() {
assert_eq!(dna::RibonucleicAcid::new("U"), dna::DeoxyribonucleicAcid::new("A").to_rna());
assert_eq!(Ok(dna::RibonucleicAcid::new("U")), dna::DeoxyribonucleicAcid::new("A").to_rna());
}

#[test]
#[ignore]
fn test_transcribes_thymine_to_adenine() {
assert_eq!(dna::RibonucleicAcid::new("A"), dna::DeoxyribonucleicAcid::new("T").to_rna());
assert_eq!(Ok(dna::RibonucleicAcid::new("A")), dna::DeoxyribonucleicAcid::new("T").to_rna());
}

#[test]
#[ignore]
fn test_transcribes_all_dna_to_rna() {
assert_eq!(dna::RibonucleicAcid::new("UGCACCAGAAUU"), dna::DeoxyribonucleicAcid::new("ACGTGGTCTTAA").to_rna())
assert_eq!(Ok(dna::RibonucleicAcid::new("UGCACCAGAAUU")), dna::DeoxyribonucleicAcid::new("ACGTGGTCTTAA").to_rna())
}

#[test]
#[ignore]
fn handles_invalid_input() {
assert!(dna::DeoxyribonucleicAcid::new("U").to_rna().is_err());
}

#[test]
#[ignore]
fn handles_completely_invalid_input() {
assert!(dna::DeoxyribonucleicAcid::new("XXX").to_rna().is_err());
}

#[test]
#[ignore]
fn handles_partially_invalid_input() {
assert!(dna::DeoxyribonucleicAcid::new("ACGTXXXCTTAA").to_rna().is_err());
}