• 2 Posts
  • 1 Comment
Joined 1M ago
cake
Cake day: Apr 18, 2022

help-circle
rss
E0515: Returns a value referencing data owned by the current function
I'm getting a `returns a value referencing data owned by the current function` on line 17 on `.unwrap_or("/tmp/")`. This is the first time I try something on rust after finishing the book. I still don't know enough to debug this. ``` use clap::{Arg, Command}; fn main() { args = parse_args(); println!("The directory passed is: {}", args[0]); } fn parse_args() -> Vec<&str> { let matches = Command::new("Parse Torrent") .arg(Arg::new("dir") .short('f') .long("dir") .takes_value(true) .help("input directory of your computer, example /disk1/dir1/")) .get_matches(); matches.value_of(dir).unwrap_or("/tmp/") } ```
fedilink

I want to get the names and the percentages from the string. I’ve managed to get the percentage by substituting match for search. But I’m still getting an error while getting the name.


[SOLVED] Get list of strings from a messed multiline string
I want to do the following but I don't know how to get the name and percentage: `mymodule_test.py` ``` import unittest from unittest import TestCase from mymodule import get_wanted_cards, get_percentage class GetCardsTestCase(TestCase): def test_get_wanted_cards(self): s = ''' ↑ A Little Chat 92 (1%) 0 (0%) NEW ↑ An Offer You Can't Refuse 88 (87%) 0 (0%) NEW ↑ Angelic Observer 92 (91%) 0 (0%) NEW ''' expected = ["4 An Offer You Can't Refuse", "4 Angelic Observer"] actual = get_wanted_cards(s) self.assertEqual(actual, expected) def test_get_percentage(self): s = "92 (1%)" expected = 1 actual = get_percentage(s) self.assertEqual(actual, expected) if __name__ == '__main__': unittest.main() ``` `mymodule.py` ``` import re from typing import List def get_wanted_cards(s: str) -> List[str]: res = [] for line in s.splitlines(): array = line.split("\t") if len(array) < 5: continue name = array[1] percent = get_percentage(array[2]) if percent >= 50: res += "4 " + name return res def get_percentage(s: str) -> int: return int(re.match(r'\(([0-9]*)%\)', s).group(1)) if __name__ == "__main__": pass ```
fedilink