Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1604 connectés 

  FORUM HardWare.fr
  Programmation
  Divers

  [Rust] borrowed value does not live long enough

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Rust] borrowed value does not live long enough

n°2262673
kao98
...
Posté le 17-07-2015 à 15:40:00  profilanswer
 

Y'a t'il des spécialistes rust dans le coin ?
 
J'essaie de m'y mettre gentiement. J'ai vaguement compris le principe de scope, de borrow, toussa.
 
Néanmoins, je suis bloqué sur une macro toute bête :
 

Code :
  1. use std::path::Path;
  2. macro_rules! file_name_1 {
  3.    ($path:expr) => {{
  4.        let path = Path::new($path);
  5.        path.file_name().unwrap().to_str().unwrap()
  6.    }};
  7. }
  8. macro_rules! file_name_2 {
  9.    ($path:expr) => {{
  10.        let unix_path = $path.replace("\\", "/" );
  11.        let path = Path::new(&unix_path);
  12.        path.file_name().unwrap().to_str().unwrap()
  13.    }};
  14. }
  15. fn main() {
  16.    println!("{}", file_name_1!("/home/user/file.txt" ));
  17.    println!("{}", file_name_2!("c:\\home\\user\\file.txt" ));
  18. }


 


error: `unix_path` does not live long enough
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
<std macros>:2:1: 2:58 note: reference must be valid for the call at 2:0...
<std macros>:2 $ crate:: io:: _print ( format_args ! ( $ ( $ arg ) * ) ) ) ;
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
error: aborting due to previous error


 
Je comprends le problème. Mais je ne vois pas de solution simple pour obenir une macro simple qui me retourne le file_name d'un chemin donné, que le chemin soit de type unix ou windows.
 
Avez-vous des idées ?


---------------
Kao ..98 - Uplay (R6S) : kao98.7.62x39 - Origin (BF4, BF1) : kntkao98
mood
Publicité
Posté le 17-07-2015 à 15:40:00  profilanswer
 

n°2262712
kao98
...
Posté le 18-07-2015 à 10:19:35  profilanswer
 

Trouvé !
 

Code :
  1. use std::path::PathBuf;
  2.    macro_rules! file_name {
  3.        ($path:expr) => {{
  4.            PathBuf::from($path.replace("\\", "/" ))
  5.                .file_name()
  6.                .unwrap()
  7.                .to_str()
  8.                .unwrap()
  9.        }};
  10.    }
  11.    fn main() {
  12.        println!("{}", file_name!("/home/user/file.txt" ));
  13.        println!("{}", file_name!("c:\\home\\user\\file.txt" ));
  14.    }


---------------
Kao ..98 - Uplay (R6S) : kao98.7.62x39 - Origin (BF4, BF1) : kntkao98

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Divers

  [Rust] borrowed value does not live long enough

 

Sujets relatifs
Lire un packet DNS (un champ de long. variable) dans une structure ? récuperer le value of du value of d'une variable[XSLT1.0]
Temps pour performer un echo de 38kb .. longTout les x secondes/exécuter javascript et mise a jour value
PHP - Traitement long sans blocage du navigateur ?OVH Mutualisé et traitement long
Problème signature ds windows live mail[XUL] Manipulation textbox et value
editeur HTML source / live previewCHaine coupé dans le value d'un input
Plus de sujets relatifs à : [Rust] borrowed value does not live long enough


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR